Skip to content

Commit

Permalink
DEV: refactor to remove widgets, modifyClass (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomerobot authored Nov 29, 2024
1 parent 841455b commit 35626b4
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 156 deletions.
87 changes: 38 additions & 49 deletions common/common.scss
Original file line number Diff line number Diff line change
@@ -1,126 +1,119 @@
@import "common/components/buttons";

$category-badge-style: null !default; // helps detect the category badge styles theme component

:root {
// fallback variables
--composer-category-text-color: var(--secondary);
--composer-category-color: var(--tertiary);
--category-color: var(--secondary);
--category-text-color: var(--primary-low-mid);
}

@if $use-category-color-in-composer == "true" {
#reply-control {
// composer colors
.grippie {
transition: background-color 0.25s;
background-color: var(--composer-category-color, var(--primary-low-mid));
background-color: var(--composer-category-color, var(--tertiary));
&:before {
border-color: var(--composer-category-text-color, var(--secondary));
}
}

.save-or-cancel {
.btn-primary {
background-color: var(--composer-category-color);
color: var(--composer-category-text-color);
color: var(--composer-category-text-color, var(--secondary));
.d-icon {
color: var(--composer-category-text-color);
color: var(--composer-category-text-color, var(--secondary));
}
}
}
}
}

@if $use-category-color-in-header == "true" {
// header colors

.d-header {
background-color: var(--category-color);
background-color: var(--category-color, var(--header_background));
transition: background-color 0.25s;
color: var(--category-text-color);
color: var(--category-text-color, var(--header_primary));

.title {
a,
a:visited {
color: var(--category-text-color, var(--header_primary));
}
}

@if $category-badge-style == null {
// checks for category badge styles theme component
.extra-info-wrapper .badge-category__wrapper .badge-category {
color: var(--category-text-color);
&:before {
box-shadow: inset 0 0 0 1px var(--category-text-color);
}
}
}
}

.header-sidebar-toggle button,
.d-header-icons .icon {
.d-icon {
color: var(--category-text-color);
color: var(--category-text-color, var(--header_primary-low-mid));
}
.discourse-no-touch & {
&:hover,
&:focus {
background: var(--category-text-color);
color: var(--category-text-color, var(--header_primary-low));
.d-icon {
color: var(--category-color);
color: var(--category-color, var(--header_primary-medium));
}
}
}
}

.drop-down-mode .d-header-icons .active .icon .d-icon {
color: var(--category-color);
color: var(--category-color, var(--header_primary-medium));
}

.extra-info-wrapper {
.badge-wrapper.bullet span.badge-category,
.badge-wrapper.bar span.badge-category,
.badge-wrapper.none span.badge-category,
.topic-link {
color: var(--category-text-color);
color: var(--category-text-color, var(--header_primary-high));
.d-icon {
color: currentColor;
}
}
.discourse-tag {
color: var(
--category-text-color
--category-text-color,
var(--header_primary-high)
) !important; // override important in core
}

.badge-category-bg,
.badge-category-parent-bg {
border: 1px solid var(--category-text-color);
}

.badge-category-parent-bg {
border-right: 0;
+ .badge-category-bg {
border-left: 0;
}
}

.title-wrapper .topic-statuses .d-icon {
color: var(--category-text-color);
color: var(--category-text-color, var(--header_primary-high));
}
}
}

@if $use-category-color-in-timeline == "true" {
// timeline colors

.timeline-container .topic-timeline .timeline-scrollarea {
border-color: var(--primary-low);
border-color: var(--primary-low, var(--tertiary-low-or-tertiary-high));
}

.timeline-container .topic-timeline .timeline-handle {
background: var(--category-color);
background: var(--category-color, var(--tertiary-low-or-tertiary-high));
}
}

@if $use-category-color-in-buttons == "true" {
// some buttons

body.category #create-topic,
#create-topic,
#topic-footer-buttons .btn.btn-primary {
@include btn(
$text-color: var(--category-text-color),
$bg-color: var(--category-color),
$icon-color: var(--category-text-color),
$hover-text-color: var(--category-text-color),
$hover-bg-color: var(--category-color),
$hover-icon-color: var(--category-text-color)
$text-color: var(--category-text-color, var(--primary)),
$bg-color: var(--category-color, var(--primary-low)),
$icon-color: var(--category-text-color, var(--primary-high)),
$hover-text-color: var(--category-text-color, var(--secondary)),
$hover-bg-color: var(--category-color, var(--primary-medium)),
$hover-icon-color: var(--category-text-color, var(--secondary))
);
}

Expand All @@ -129,8 +122,4 @@
#topic-footer-buttons .btn.btn-primary {
margin-bottom: 0.5em;
}

body.category #create-topic {
margin-left: 0.5em;
}
}
10 changes: 10 additions & 0 deletions javascripts/discourse/api-initializers/category-color.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { apiInitializer } from "discourse/lib/api";
import CategoryColor from "../components/category-color";
import ComposerCategoryColor from "../components/composer-category-color";

export default apiInitializer("1.14.0", (api) => {
api.renderInOutlet("above-site-header", <template>
<CategoryColor />
<ComposerCategoryColor />
</template>);
});
15 changes: 0 additions & 15 deletions javascripts/discourse/api-initializers/category-color.js

This file was deleted.

37 changes: 0 additions & 37 deletions javascripts/discourse/api-initializers/composer-color.js

This file was deleted.

60 changes: 60 additions & 0 deletions javascripts/discourse/components/category-color.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Component from "@glimmer/component";
import { action } from "@ember/object";
import { service } from "@ember/service";
import Category from "discourse/models/category";

function applyCategoryColors(color, textColor) {
if (color && textColor) {
document.body.style.setProperty("--category-color", `#${color}`);
document.body.style.setProperty("--category-text-color", `#${textColor}`);
}
}

function clearCategoryColors() {
document.body.style.removeProperty("--category-color");
document.body.style.removeProperty("--category-text-color");
}

export default class CategoryColor extends Component {
@service router;

constructor() {
super(...arguments);
this.setCategoryColors();
this.router.on("routeDidChange", this.setCategoryColors);
}

willDestroy() {
super.willDestroy(...arguments);
this.router.off("routeDidChange", this.setCategoryColors);
}

get isCategoryRoute() {
return this.router.currentRoute.params.hasOwnProperty(
"category_slug_path_with_id"
);
}

get isTopicRoute() {
return this.router.currentRoute.name.includes("topic.fromParams");
}

@action
setCategoryColors() {
let category;

if (this.isCategoryRoute) {
category = this.router.currentRoute.attributes.category;
} else if (this.isTopicRoute) {
const categoryId =
this.router.currentRoute.parent?.attributes?.category_id;
category = Category.findById(categoryId);
}

if (category) {
applyCategoryColors(category.color, category.text_color);
} else {
clearCategoryColors();
}
}
}
79 changes: 79 additions & 0 deletions javascripts/discourse/components/composer-category-color.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Component from "@glimmer/component";
import { action } from "@ember/object";
import { addObserver, removeObserver } from "@ember/object/observers";
import { service } from "@ember/service";
import Category from "discourse/models/category";

export default class ComposerCategoryColor extends Component {
@service composer;
@service appEvents;

currentCategory = null;

constructor() {
super(...arguments);

this.appEvents.on("composer:will-open", this.handleComposerOpened);
}

willDestroy() {
super.willDestroy(...arguments);

this.appEvents.off("composer:will-open", this.handleComposerOpened);
this.removeCategoryObserver();
}

observeCategoryChanges() {
if (this.composer.model) {
addObserver(this.composer.model, "categoryId", this, this.updateCategory);
}
}

removeCategoryObserver() {
if (this.composer.model) {
removeObserver(
this.composer.model,
"categoryId",
this,
this.updateCategory
);
}
}

get categoryId() {
return this.composer.model?.get("categoryId");
}

@action
handleComposerOpened() {
this.observeCategoryChanges();
this.updateCategory();
}

@action
updateCategory() {
const categoryId = this.categoryId;
const category = Category.findById(categoryId);

if (category) {
this.currentCategory = category;
document.body.style.setProperty(
"--composer-category-color",
`#${category.color}`
);
document.body.style.setProperty(
"--composer-category-text-color",
`#${category.text_color}`
);
} else {
this.clearCategoryColors();
}
}

@action
clearCategoryColors() {
this.currentCategory = null;
document.body.style.removeProperty("--composer-category-color");
document.body.style.removeProperty("--composer-category-text-color");
}
}

This file was deleted.

Loading

0 comments on commit 35626b4

Please sign in to comment.