mirror of
https://github.com/discourse/discourse.git
synced 2025-02-20 17:52:45 +08:00
DEV: Modernize <AddCategoryTagClasses>
to use bodyClass helper (#23703)
This commit is contained in:
parent
43791ea2b5
commit
3fe5ce7ab4
@ -0,0 +1,14 @@
|
||||
import bodyClass from "discourse/helpers/body-class";
|
||||
import { concat } from "@ember/helper";
|
||||
|
||||
const AddCategoryTagClasses = <template>
|
||||
{{#if @category}}
|
||||
{{bodyClass "category" (concat "category-" @category.fullSlug)}}
|
||||
{{/if}}
|
||||
|
||||
{{#each @tags as |tag|}}
|
||||
{{bodyClass (concat "tag-" tag)}}
|
||||
{{/each}}
|
||||
</template>;
|
||||
|
||||
export default AddCategoryTagClasses;
|
@ -1,44 +0,0 @@
|
||||
import Component from "@ember/component";
|
||||
import { scheduleOnce } from "@ember/runloop";
|
||||
|
||||
export default class extends Component {
|
||||
tagName = "";
|
||||
currentClasses = new Set();
|
||||
|
||||
didReceiveAttrs() {
|
||||
scheduleOnce("afterRender", this, this._updateClasses);
|
||||
}
|
||||
|
||||
willDestroyElement() {
|
||||
scheduleOnce("afterRender", this, this._removeClasses);
|
||||
}
|
||||
|
||||
_updateClasses() {
|
||||
if (this.isDestroying || this.isDestroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const desiredClasses = new Set();
|
||||
|
||||
const slug = this.category?.fullSlug;
|
||||
if (slug) {
|
||||
desiredClasses.add("category");
|
||||
desiredClasses.add(`category-${slug}`);
|
||||
}
|
||||
this.tags?.forEach((t) => desiredClasses.add(`tag-${t}`));
|
||||
|
||||
document.body.classList.add(...desiredClasses);
|
||||
|
||||
const removeClasses = [...this.currentClasses].filter(
|
||||
(c) => !desiredClasses.has(c)
|
||||
);
|
||||
|
||||
document.body.classList.remove(...removeClasses);
|
||||
|
||||
this.currentClasses = desiredClasses;
|
||||
}
|
||||
|
||||
_removeClasses() {
|
||||
document.body.classList.remove(...this.currentClasses);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user