-
-
Notifications
You must be signed in to change notification settings - Fork 17
[#279] Fix position-try demos and polyfill logic #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,7 +161,7 @@ | |
| selector: string, | ||
| tactics: PositionTryOptionsTryTactics[], | ||
| ) { | ||
| // todo: This currently only uses the styles from the first match. Each | ||
| // element may have different styles and need a separate fallback definition. | ||
| const el: HTMLElement | null = document.querySelector(selector); | ||
| if (el) { | ||
|
|
@@ -275,7 +275,7 @@ | |
| end: 'start', | ||
| }, | ||
| 'flip-start': { | ||
| // TODO: Requires fuller logic | ||
| }, | ||
| }; | ||
|
|
||
|
|
@@ -300,7 +300,7 @@ | |
| tactic: PositionTryOptionsTryTactics, | ||
| ) { | ||
| if (tactic === 'flip-start') { | ||
| // TODO: Handle flip-start | ||
| return prop; | ||
| } else { | ||
| const mapping = PositionAreaPropertyMapping[tactic]; | ||
|
|
@@ -316,7 +316,7 @@ | |
| valueAst: Value, | ||
| tactic: PositionTryOptionsTryTactics, | ||
| ) { | ||
| // TODO: Handle flip-start | ||
| if (key === 'margin') { | ||
| const [first, second, third, fourth] = valueAst.children.toArray(); | ||
| if (tactic === 'flip-block') { | ||
|
|
@@ -371,7 +371,7 @@ | |
| declarations[key] ??= 'revert'; | ||
| } | ||
|
|
||
| // todo: This does not support percentage anchor-side values, nor anchor | ||
| // functions that are passed through custom properties. | ||
| walk(valueAst, { | ||
| visit: 'Function', | ||
|
|
@@ -576,11 +576,19 @@ | |
| // Parse `position-try`, `position-try-order`, and | ||
| // `position-try-fallbacks` declarations | ||
| const { order, options } = getPositionFallbackValues(node); | ||
| const anchorPosition: AnchorPosition = {}; | ||
| if (order) { | ||
| anchorPosition.order = order; | ||
| } | ||
|
Comment on lines
-579
to
-582
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude's explanation of this change: In the polyfill, a comma-separated selector list that uses CauseIn FixGive each selector its own
TestsAdded a regression unit test asserting that each selector in a shared rule gets only its own try-tactic fallback. Existing unit (186) and e2e (37) suites pass. |
||
| selectors.forEach(({ selector }) => { | ||
| // Each selector in a comma-separated list gets its own position, so | ||
| // fallbacks generated for one selector don't leak onto another. This | ||
| // matters for try-tactics, whose fallbacks are keyed per selector | ||
| // (`${selector}-${tactics}`); without per-selector scoping, the | ||
| // second target ends up trying the first target's fallbacks. See | ||
| // https://github.com/oddbird/css-anchor-positioning/issues/279. | ||
| const anchorPosition: AnchorPosition = {}; | ||
| if (order) { | ||
| anchorPosition.order = order; | ||
| } | ||
| // Track which fallbacks have been added to *this* selector. | ||
| const selectorFallbacksAdded = new Set<string>(); | ||
| options?.forEach((tryObject) => { | ||
| let name; | ||
| // Apply try fallback | ||
|
|
@@ -623,12 +631,19 @@ | |
| fallbackTargets[dataAttr] ??= []; | ||
| fallbackTargets[dataAttr].push(selector); | ||
|
|
||
| if (!fallbacksAdded.has(name)) { | ||
| // Add the fallback to this selector's position (deduped per | ||
| // selector). | ||
| if (!selectorFallbacksAdded.has(name)) { | ||
| selectorFallbacksAdded.add(name); | ||
| anchorPosition.fallbacks ??= []; | ||
| anchorPosition.fallbacks.push(fallbacks[name]); | ||
| } | ||
|
|
||
| // Inject the generated `@position-try` block once per stylesheet, | ||
| // scoped to a unique data-attr. | ||
| if (!fallbacksAdded.has(name)) { | ||
| fallbacksAdded.add(name); | ||
|
|
||
| // Add `@position-try` block, scoped to a unique data-attr | ||
| this.stylesheet?.children.prependData({ | ||
| type: 'Rule', | ||
| prelude: { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about
CSS.registerProperty🎉