# Modifying style attribute dynamically on Topic List Item

**URL:** https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415
**Category:** Development
**Created:** [January 9, 2025, 11:08pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415 "2025-01-09T23:08:10Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 9, 2025, 11:08pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/1 "2025-01-09T23:08:10Z")

</div>

Disclaimer: very happy with the direction here and thanks for all the work so far as there’s a lot of power in the new system. Just need to make sure we keep some equivalent capabilities? …

> [@Upcoming topic-list changes - how to prepare themes and plugins](https://meta.discourse.org/t/upcoming-topic-list-changes-how-to-prepare-themes-and-plugins/343404/1):
>
> ## 🆘 What about other customizations?
> 
> If your customization cannot be achieved using the new APIs we’ve introduced, please let us know by creating a new #Development topic to discuss.

In my Component, TLP, I’m currently setting the style attribute of the Topic List Item outer tag dynamically:

- setting the `background:` colour to a unique RGB value to match the dominant colour of the image. This should ideally be done in Ember loop.

I’m also setting setting `grid-row-end` to very quickly determine the size of the masonry element. This is very fast and efficient and avoids a lot of Javascript. However, for this I’m using Javascript as depends on the final sizes in the DOM.

Currently these co-exist happily.

![image](https://global.discourse-cdn.com/meta/original/4X/1/7/8/17855cf42e3fc521b0fd96bd6ff372aea8fae089.png)

`<tr style="background: rgb(56, 10, 58); grid-row-end: span 48;" data-topic-id="23321" id="ember168" class="topic-list-item category-uncategorized tag-inforunners visited white-text has-thumbnail tiles-grid-item ember-view" data-is-last-viewed-topic="true">`

These are both critical customisations to the Theme Component and without them, it would not be supportable.

This first amendment to style is currently achieved with a direct style attribute binding with modifyClass, which I understand is a deprecated method.

> <https://github.com/merefield/discourse-tc-topic-list-previews/blob/2e7dadadc14bf7fe9c2fd8f123bd3ecbb36e49af/javascripts/discourse/initializers/preview-edits.js.es6#L187>

Would it be possible to have an API in the new world for this please?

Or perhaps I’ve missed an existing way to swing this?

However this is not something I believe can be achieved with classes.

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [January 10, 2025, 6:52am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/2 "2025-01-10T06:52:39Z")

</div>

> [@merefield](#):
>
> which I understand is a deprecated method.

Is it? 😕 It’s not just require to update to the native class syntax?

> [@Using modifyClass to change core behavior](https://meta.discourse.org/t/using-modifyclass-to-change-core-behavior/262064):
>
> For advanced themes and plugins, Discourse offers the modifyClass system. This allows you to extend and override functionality in many of core’s javascript classes. When to use modifyClass modifyClass should be a last resort, when your customization cannot be made via Discourse’s more stable customization APIs (e.g. plugin-api methods, plugin outlets, transformers). Core’s code can change at any time. And therefore, customizations made via modifyClass could break at any time. When using this A…

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 10, 2025, 6:54am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/3 "2025-01-10T06:54:16Z")

</div>

> [@Upcoming topic-list changes - how to prepare themes and plugins](https://meta.discourse.org/t/upcoming-topic-list-changes-how-to-prepare-themes-and-plugins/343404/1):
>
> Upgrades will also be required if you use modifyClass on `component:topic-list` or `component:topic-list-item`.
> 
> If your site has any of these incompatible customizations,

I’m going by this

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [January 10, 2025, 6:56am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/4 "2025-01-10T06:56:02Z")

</div>

Oh, I see. I thought that meant this upgrade.

> [@Don](#):
>
> It’s not just require to update to the native class syntax?

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 10, 2025, 7:01am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/5 "2025-01-10T07:01:14Z")

</div>

It would be good to get clarity.

- So is `modifyClass` still valid? (Just updated syntax - might be good if so to reference that guide)
- Can I still use that to access the style attribute of the enclosing tag?

Classic Components are different in that they historically provided access to enclosing tag level attributes outside of the template so they were straightforward to modify dynamically in code.

---

<div class="post-metadata">

### Author: ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)
#### Post date: [January 10, 2025, 7:34am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/6 "2025-01-10T07:34:40Z")

</div>

Ah, you’re right. There is a deprecated notice in console.

> Deprecation notice: Modifying ‘component:topic-list-item’ with ‘modifyClass’ is deprecated. Use the value transformer ‘topic-list-columns’ and other new topic-list plugin APIs instead.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 10, 2025, 9:52am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/7 "2025-01-10T09:52:16Z")

</div>

`modifyClass` itself is not deprecated. It’s a risky customization method, and you should take precautions when using it (as described in the doc @don linked), but we don’t have any imminent plans to remove it.

The reason we’re printing a deprecation message for `component:topic-list` and `component:topic-list-items` is that those components are the legacy implementation of the topic list, and are not used when the glimmer topic list is enabled. Now, we have a set of glimmer components like `components/topic-list/item`.

So, technically, you could use `modifyClass` on those new components. But, because they’re glimmer components, you won’t be able to modify the style tag via the JS class, so it won’t help with the problem in the OP.

> [@merefield](#):
>
> Or perhaps I’ve missed an existing way to swing this?
> 
> However this is not something I believe can be achieved with classes.

We have a similar ‘masonry’ mode in the official d-topic-thumbnails theme component, so it’s worth looking at how that’s implemented. We add a unique class to each ‘row’ of the topic list:

> <https://github.com/discourse/discourse-topic-thumbnails/blob/4e4fc54ace3cfdce2299c74c03c631780363d7b9/javascripts/discourse/initializers/topic-thumbnails-init.gjs#L49-L57>

Then we render a dynamic `<style>` tag in a separate plugin-outlet, which targets those class names:

> <https://github.com/discourse/discourse-topic-thumbnails/blob/4e4fc54ace3cfdce2299c74c03c631780363d7b9/javascripts/discourse/connectors/before-topic-list-body/topic-list-masonry-styles.gjs#L53-L60>

That said… it is a little convoluted, so perhaps we should look at adding a specific valueTransformer to allow changing the `style=` attribute. It’s a _little_ tricky because of Ember’s xss/htmlSafe protections on that attribute… but I’m sure we could get something working.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 11, 2025, 10:58am UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/8 "2025-01-11T10:58:09Z")

</div>

Thanks for considering this David, that would be great!

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 30, 2025, 1:38pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/9 "2025-01-30T13:38:17Z")

</div>

David, I have another one, would it be possible to hook into Glimmer events such as didInsert, didUpdate, willDestroy via the new API?

I want to run some additional javascript on didInsert, for example, which is currently handled by @on and modifyClass.

> <https://github.com/merefield/discourse-tc-topic-list-previews/blob/2e7dadadc14bf7fe9c2fd8f123bd3ecbb36e49af/javascripts/discourse/initializers/preview-edits.js.es6#L112>

(happy to make this a new Topic btw)

I’ll try your technique here for now:

> <https://github.com/discourse/discourse-topic-thumbnails/blob/4e4fc54ace3cfdce2299c74c03c631780363d7b9/javascripts/discourse/connectors/before-topic-list-body/topic-list-masonry-styles.gjs#L6>

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 30, 2025, 5:02pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/10 "2025-01-30T17:02:27Z")

</div>

> [@merefield](#):
>
> would it be possible to hook into Glimmer events such as didInsert, didUpdate, willDestroy via the new API?

We could introduce specific transformers in certain places. But I don’t see us adding a general API which would allow you to attach to any element/component.

As for the style= attribute discussion, I made this PR. Will try to get it reviewed/merged:

[https://github.com/discourse/discourse/pull/30727](https://github.com/discourse/discourse/pull/30727)

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 30, 2025, 5:05pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/11 "2025-01-30T17:05:06Z")

</div>

> [@david](#):
>
> We could introduce specific transformers in certain places. But I don’t see us adding a general API which would allow you to attach to any element/component.

Thanks. So far so good with your current technique, I’ve tailored it for TLP and it appears to function well. I’ll revert if I find any more potential gaps.

> [@david](#):
>
> As for the style= attribute discussion, I made this PR. Will try to get it reviewed/merged:

Awesome! I should be ready for it by the time that’s merged, looking forward to it! thanks David!

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [January 31, 2025, 4:24pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/12 "2025-01-31T16:24:16Z")

</div>

Works like a _charm_, thanks again! 🚀

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [March 2, 2025, 4:24pm UTC](https://meta.discourse.org/t/modifying-style-attribute-dynamically-on-topic-list-item/346415/13 "2025-03-02T16:24:38Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
