Skip to content

Commit b4270fb

Browse files
committed
Allow multiple position-area declarations for a single selector
1 parent edbf927 commit b4270fb

File tree

2 files changed

+40
-54
lines changed

2 files changed

+40
-54
lines changed

Diff for: src/parse.ts

+39-53
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ export interface PositionAreaDeclaration {
7878
wrapperEl?: HTMLElement | null;
7979
targetEl?: HTMLElement | null;
8080
anchorEl?: HTMLElement | PseudoElement | null;
81-
anchorName?: string;
82-
customPropName?: string;
8381
targetUUID: string;
8482
}
85-
type PositionAreaDeclarations = Record<string, PositionAreaDeclaration>;
83+
84+
// `key` is the target element selector
85+
// `value` is the position-area data for that property
86+
type PositionAreaDeclarations = Record<string, PositionAreaData[]>;
8687

8788
export interface AnchorPosition {
8889
declarations?: AnchorFunctionDeclaration;
89-
positionAreas?: PositionAreaDeclaration[];
9090
fallbacks?: TryBlock[];
9191
order?: PositionTryOrder;
9292
}
@@ -278,10 +278,10 @@ function getPositionAreaData(node: CssNode, block: Block | null) {
278278

279279
async function getAnchorEl(
280280
targetEl: HTMLElement | null,
281-
anchorObj: AnchorFunction | PositionAreaDeclaration,
281+
anchorObj?: AnchorFunction,
282282
) {
283-
let anchorName = anchorObj.anchorName;
284-
const customPropName = anchorObj.customPropName;
283+
let anchorName = anchorObj?.anchorName;
284+
const customPropName = anchorObj?.customPropName;
285285
if (targetEl && !anchorName) {
286286
const positionAnchorProperty = getCSSPropertyValue(
287287
targetEl,
@@ -364,10 +364,10 @@ export async function parseCSS(styleData: StyleData[]) {
364364

365365
if (positionAreaData) {
366366
for (const { selector } of selectors) {
367-
positionAreas[selector] = {
368-
...positionAreas[selector],
369-
positionArea: positionAreaData,
370-
};
367+
positionAreas[selector] = [
368+
...(positionAreas[selector] ?? []),
369+
positionAreaData,
370+
];
371371
}
372372
}
373373
if (updated || positionAreaData?.changed) {
@@ -760,48 +760,34 @@ export async function parseCSS(styleData: StyleData[]) {
760760
document.querySelectorAll(targetSel);
761761
for (const targetEl of targets) {
762762
// For every target element, find a valid anchor element
763-
const targetUUID = `--pa-target-${nanoid(12)}`;
764-
const anchorObj = {
765-
targetUUID,
766-
positionArea: { ...positions.positionArea },
767-
fallbackValue: '',
768-
};
769-
770-
const wrapperEl = wrapperForPositionedElement(targetEl, targetUUID);
771-
positionAreaMappingStyleElement.css += activeWrapperStyles(
772-
targetUUID,
773-
positions.positionArea.selectorUUID,
774-
);
775-
positionAreaMappingStyleElement.changed = true;
776-
const anchorEl = await getAnchorEl(targetEl, anchorObj);
777-
const uuid = `--anchor-${nanoid(12)}`;
778-
// Store new mapping, in case inline styles have changed and will
779-
// be overwritten -- in which case new mappings will be re-added
780-
inlineStyles.set(targetEl, {
781-
...(inlineStyles.get(targetEl) ?? {}),
782-
[anchorObj.targetUUID]: targetUUID,
783-
});
784-
// Point original uuid to new uuid
785-
targetEl.setAttribute(
786-
'style',
787-
`${anchorObj.targetUUID}: var(${uuid}); ${
788-
targetEl.getAttribute('style') ?? ''
789-
}`,
790-
);
791-
const targetProperty = 'position-area';
792-
// Populate new data for each anchor/target combo
793-
validPositions[targetSel] = {
794-
...validPositions[targetSel],
795-
positionAreas: [positions],
796-
declarations: {
797-
...validPositions[targetSel]?.declarations,
798-
[targetProperty]: [
799-
...(validPositions[targetSel]?.declarations?.[targetProperty] ??
800-
[]),
801-
{ ...anchorObj, anchorEl, targetEl, wrapperEl, uuid },
802-
],
803-
},
804-
};
763+
const anchorEl = await getAnchorEl(targetEl);
764+
for (const positionData of positions) {
765+
const targetUUID = `--pa-target-${nanoid(12)}`;
766+
const wrapperEl = wrapperForPositionedElement(targetEl, targetUUID);
767+
positionAreaMappingStyleElement.css += activeWrapperStyles(
768+
targetUUID,
769+
positionData.selectorUUID,
770+
);
771+
positionAreaMappingStyleElement.changed = true;
772+
// Populate new data for each anchor/target combo
773+
validPositions[targetSel] = {
774+
...validPositions[targetSel],
775+
declarations: {
776+
...validPositions[targetSel]?.declarations,
777+
'position-area': [
778+
...(validPositions[targetSel]?.declarations?.['position-area'] ??
779+
[]),
780+
{
781+
targetUUID,
782+
positionArea: positionData,
783+
anchorEl,
784+
targetEl,
785+
wrapperEl,
786+
} as PositionAreaDeclaration,
787+
],
788+
},
789+
};
790+
}
805791
}
806792
}
807793

Diff for: src/polyfill.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ export async function polyfill(
594594
let styleData = await fetchCSS(options.elements, options.excludeInlineStyles);
595595

596596
// pre parse CSS styles that we need to cascade
597-
const cascadeCausedChanges = await cascadeCSS(styleData);
597+
const cascadeCausedChanges = cascadeCSS(styleData);
598598
if (cascadeCausedChanges) {
599599
styleData = await transformCSS(styleData);
600600
}

0 commit comments

Comments
 (0)