Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"@github/relative-time-element": "^4.5.0",
"@github/tab-container-element": "^4.8.2",
"@lit-labs/react": "1.2.1",
"@oddbird/css-anchor-positioning": "^0.9.0",
"@oddbird/popover-polyfill": "^0.5.2",
"@primer/behaviors": "^1.10.2",
"@primer/live-region-element": "^0.7.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@
/* stylelint-disable primer/spacing */
top: calc(anchor(bottom) + var(--base-size-4));
left: anchor(left);
max-height: calc(100dvh - anchor(bottom) - var(--base-size-4));
}

&[data-side='outside-top'] {
margin-bottom: var(--base-size-4);
bottom: anchor(top);
left: anchor(left);
max-height: calc(anchor(top) - var(--base-size-4));
}

&[data-side='outside-left'] {
right: anchor(left);
top: anchor(top);
margin-right: var(--base-size-4);
max-height: calc(100dvh - anchor(top));
}

&[data-side='outside-right'] {
left: anchor(right);
top: anchor(top);
margin-left: var(--base-size-4);
max-height: calc(100dvh - anchor(top));
}
}
26 changes: 6 additions & 20 deletions packages/react/src/AnchoredOverlay/AnchoredOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,6 @@ export type AnchoredOverlayProps = AnchoredOverlayBaseProps &
(AnchoredOverlayPropsWithAnchor | AnchoredOverlayPropsWithoutAnchor) &
Partial<Pick<PositionSettings, 'align' | 'side' | 'anchorOffset' | 'alignmentOffset' | 'displayInViewport'>>

const applyAnchorPositioningPolyfill = async () => {
if (typeof window !== 'undefined' && !('anchorName' in document.documentElement.style)) {
try {
await import('@oddbird/css-anchor-positioning')
} catch (e) {
// eslint-disable-next-line no-console
console.warn('Failed to load CSS anchor positioning polyfill:', e)
}
}
}

const defaultVariant = {
regular: 'anchored',
narrow: 'anchored',
Expand Down Expand Up @@ -173,7 +162,9 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
displayCloseButton = true,
closeButtonProps = defaultCloseButtonProps,
}) => {
const cssAnchorPositioning = useFeatureFlag('primer_react_css_anchor_positioning')
const cssAnchorPositioningFlag = useFeatureFlag('primer_react_css_anchor_positioning')
const supportsNativeCSSAnchorPositioning = useRef(false)
const cssAnchorPositioning = cssAnchorPositioningFlag && supportsNativeCSSAnchorPositioning.current
const anchorRef = useProvidedRefOrCreate(externalAnchorRef)
const [overlayRef, updateOverlayRef] = useRenderForcingRef<HTMLDivElement>()
const anchorId = useId(externalAnchorId)
Expand Down Expand Up @@ -232,19 +223,14 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
[overlayRef.current],
)

const hasLoadedAnchorPositioningPolyfill = useRef(false)

useEffect(() => {
supportsNativeCSSAnchorPositioning.current = 'anchorName' in document.documentElement.style

// ensure overlay ref gets cleared when closed, so position can reset between closing/re-opening
if (!open && overlayRef.current) {
updateOverlayRef(null)
}

if (cssAnchorPositioning && !hasLoadedAnchorPositioningPolyfill.current) {
applyAnchorPositioningPolyfill()
hasLoadedAnchorPositioningPolyfill.current = true
}
}, [open, overlayRef, updateOverlayRef, cssAnchorPositioning])
}, [open, overlayRef, updateOverlayRef])

useFocusZone({
containerRef: overlayRef,
Expand Down
Loading