Add a custom selector for dark mode - #243
Conversation
- Adds a new attribute `data-ea-dark-selector` which when set will be monitored and toggle light/dark mode based on changes to it. This is for sites that use custom themes that don't use `html.dark` or similar patterns. - Note that the `adaptive-css` class is *NOT* required if this custom attribute is set - Add docs for the placement
|
This seems like a good approach to make something a bit more generic, though I doubt many publishers will use it, but at least we have a solution. |
Ya, it would give us a solution for just about any theme. |
|
I tested this out, added a few notes, and added testing instructions. I think this is close to merge-ready. |
ericholscher
left a comment
There was a problem hiding this comment.
Looks great -- AI review only caught this one suggestion.
|
|
||
| .. code:: html | ||
|
|
||
| <div class="raised" data-ea-publisher="..." data-ea-dark-selector="body[data-md-color-scheme='slate']"></div> |
There was a problem hiding this comment.
Definitely don't love the mixed quoting here, but unavoidable?
There was a problem hiding this comment.
Ya, I don't think it's avoidable but should be rare.
| @@ -1259,6 +1329,10 @@ export function unload_placements() { | |||
| elements.forEach((div) => { | |||
| div.innerHTML = ""; | |||
| div.classList.remove("loaded"); | |||
There was a problem hiding this comment.
| div.classList.remove("loaded"); | |
| div.classList.remove("loaded", "dark"); |
Ai suggested:
applyDarkSelector() toggles the dark class on the placement wrapper itself, but unload_placements() only removes loaded and disconnects the observer. It doesn’t remove dark, so a placement that was previously dark can keep that class across unload/reload cycles and briefly or persistently render with stale theme state.
There was a problem hiding this comment.
I don't think we want this suggestion. When the placement is explicitly set to dark mode (say, by the user explicitly) and then the ad is rotated for example, we want to keep it dark mode.
data-ea-dark-selectorwhich when set will be monitored and toggle light/dark mode based on changes to it. This is for sites that use custom themes that don't usehtml.darkor similar patterns.adaptive-cssclass is NOT required if this custom attribute is setbodyorhtml, then the ad will correctly load in dark/light mode based on the user's custom attribute/class but if the ad is rotated or the theme is custom toggled, then the watcher won't catch it because it's only watching for changes tohtml/bodyand only on attributes. It doesn't look at descendant nodes which should keep this from slowing anything down.Testing
To test this out manually, you can do the following: