Email or username:

Password:

Forgot your password?
Top-level
Bramus

Here’s a video of it in action: a callback gets executed when – and only when – any of the tracked CSS properties (`--variable1`, `--variable2`, `display`, or `border-width`) their computed value changes.

(Recorded in Safari because of that damn Chrome bug)

8 comments
Bramus

The code looks like this:

```
const properties = ['--variable1', '--variable2', 'display', 'border-width'];

const cssStyleObserver = new CSSStyleObserver(properties, console.log);

cssStyleObserver.attach(document.body);
```

That’s it 😊

Bramus

The code is based on prior art by
Artem Godin: github.com/fluorumlabs/css-var

That library only works with Custom Properties and only works with numeric values though, whereas the updated `style-observer` I created works with any property that can have any value.

Bramus

Extra shout-out to @jaffathecake as well, for suggesting “some of the discrete value animation stuff” could possibly be used …

github.com/w3c/csswg-drafts/is

Bramus

Winging back to the Chrome bug I mentioned, here’s the issue: `allow-discrete` erroneously does not work on custom properties – issues.chromium.org/issues/360

There technically is a workaround for it (by animating `content`) but I haven’t been able to consistently make it work.

Gonna see who I can poke internally to get the ball rolling on it ;)

Bramus

UPDATE: Jake found a workaround to bypass that Chrome bug: register the custom property with @​property.

That works.

But

Safari doesn’t like it. It seems to be stuck in a transition loop when the custom props is registered to contain string values ("<string>" or "*") 😕

Bramus

Here’s something nice by @matthewp built on top of all this: a Selector Matcher Observer powered by CSSStyleObserver.

When the element matches the given selector, a custom property gets flipped, triggering the callback attached to the CSSStyleObserver.

codepen.io/bramus/pen/zYVJBje

Bramus

Yesterday I collected all bugs I could find related to `transition-behavior: allow-discrete` onto a dedicated repo/site [^1] and also filed a bunch bugs.

The WebKit team at Apple didn’t waste any time and moved really fast on this one, as the PR to fix the WebKit bugs [^2] is already merged in! 🤩👏

[^1]: allow-discrete-bugs.netlify.ap
[^2]: github.com/WebKit/WebKit/pull/

Go Up