Skip to content

fix: add patchCSSOMto support setting anchor-name and position-anchor in JS - #447

Open
jpzwarte wants to merge 8 commits into
oddbird:mainfrom
jpzwarte:fix/445-cssom-not-working
Open

fix: add patchCSSOMto support setting anchor-name and position-anchor in JS#447
jpzwarte wants to merge 8 commits into
oddbird:mainfrom
jpzwarte:fix/445-cssom-not-working

Conversation

@jpzwarte

@jpzwarte jpzwarte commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #445

patchCSSOM() (opt-in, exported from /fn) makes anchorName and positionAnchor settable. It wraps the style getter to know which element a declaration belongs to, defines the two accessors on CSSStyleDeclaration, and writes the value into the element's style attribute; setProperty(), getPropertyValue() and removeProperty() take the dashed names as well. The values are also kept in a registry and re-applied at the start of each polyfill run, because any later CSSOM write reserializes the declaration block and drops them again.

anchorName in element.style becomes true. CSS.supports('anchor-name: --a') is unaffected, and is what the README now recommends for feature detection.

This PR also fixes inline styles inside a shadow root never being collected: fetchInlineStyles() only searched document. It now searches the polyfill roots too (both, since a shadow-scoped run still needs its host and any light-DOM anchors).

jpzwarte and others added 2 commits July 25, 2026 12:17
`anchor-name` and `position-anchor` assigned from JavaScript are dropped
by the CSSOM in a browser without native support, so nothing lands in the
`style` attribute the polyfill reads. The demo wires up its anchor at
runtime the way a design system component would, and does not work as a
result.

Refs oddbird#445

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@netlify

netlify Bot commented Jul 25, 2026

Copy link
Copy Markdown

Deploy Preview for anchor-polyfill ready!

Name Link
🔨 Latest commit 82b6875
🔍 Latest deploy log https://app.netlify.com/projects/anchor-polyfill/deploys/6a68a941dd12c50008e6887e
😎 Deploy Preview https://deploy-preview-447--anchor-polyfill.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 25, 2026

Copy link
Copy Markdown

Deploy Preview for anchor-position-wpt canceled.

Name Link
🔨 Latest commit 82b6875
🔍 Latest deploy log https://app.netlify.com/projects/anchor-position-wpt/deploys/6a68a941961b04000729cc3d

@jpzwarte
jpzwarte marked this pull request as ready for review July 25, 2026 11:48

@jamesnw jamesnw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly conceptual questions at this point-
Idea- instead of writing the unsupported values to the style tag, could we shift the values to custom properties? I think that would sidestep a lot of this. We could do it like the shifted properties in cascade.ts, so like --anchor-name-${INSTANCE_UUID}, and then it might just work when we need to read it.

Does this change make it work better for Lit, for instance?

This is useful for non-shadow DOM things, correct? It might be worth not tying the examples together.

Would it be possible to expose this as an option when the polyfill is run, or does it need to be applied as a separate step, so that the application can do some work before the polyfill can be run?

Comment thread src/cssom.ts Outdated
Comment thread src/cssom.ts
// patch it never sees anchors that are wired up from JavaScript.
const PATCHED_PROPERTIES = {
anchorName: 'anchor-name',
positionAnchor: 'position-anchor',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not positionArea or other anchor-related properties?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because anchor-name and position-anchor are the dynamic parts that i'm setting from Lit. Example:

<sl-button id="button">Button</sl-button>
<sl-tooltip for="button">Tooltip</sl-tooltip>

Which results in:

<sl-button id="button" style="anchor-name: --sl-tooltip-1">Button</sl-button>
<sl-tooltip for="button" style="position-anchor: --sl-tooltip-1">Tooltip</sl-tooltip>

Everything else is part of :host.

I could add more properties of course, but i'm not sure its worth it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps including at least position-area as well is a good idea. I can at least think of a web component where you could specify where it is anchored. But where do you stop?

Comment thread src/cssom.ts Outdated
@jpzwarte

Copy link
Copy Markdown
Contributor Author

Would it be possible to expose this as an option when the polyfill is run, or does it need to be applied as a separate step, so that the application can do some work before the polyfill can be run?

I think it's a matter of preference. If you run polyfill() after HTML has rendered, then it's too late. So in that case you would need to call patchCSSOM() before it. Otherwise it could be an option.

@jpzwarte

jpzwarte commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

This is useful for non-shadow DOM things, correct? It might be worth not tying the examples together.

True, but where would we put an example for it? A new cssom.html page?

@jpzwarte

Copy link
Copy Markdown
Contributor Author

Idea- instead of writing the unsupported values to the style tag, could we shift the values to custom properties? I think that would sidestep a lot of this. We could do it like the shifted properties in cascade.ts, so like --anchor-name-${INSTANCE_UUID}, and then it might just work when we need to read it.

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] anchor-name and position-anchor set via CSSOM are invisible to the polyfill

2 participants