@@ -78,15 +78,15 @@ export interface PositionAreaDeclaration {
78
78
wrapperEl ?: HTMLElement | null ;
79
79
targetEl ?: HTMLElement | null ;
80
80
anchorEl ?: HTMLElement | PseudoElement | null ;
81
- anchorName ?: string ;
82
- customPropName ?: string ;
83
81
targetUUID : string ;
84
82
}
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 [ ] > ;
86
87
87
88
export interface AnchorPosition {
88
89
declarations ?: AnchorFunctionDeclaration ;
89
- positionAreas ?: PositionAreaDeclaration [ ] ;
90
90
fallbacks ?: TryBlock [ ] ;
91
91
order ?: PositionTryOrder ;
92
92
}
@@ -278,10 +278,10 @@ function getPositionAreaData(node: CssNode, block: Block | null) {
278
278
279
279
async function getAnchorEl (
280
280
targetEl : HTMLElement | null ,
281
- anchorObj : AnchorFunction | PositionAreaDeclaration ,
281
+ anchorObj ? : AnchorFunction ,
282
282
) {
283
- let anchorName = anchorObj . anchorName ;
284
- const customPropName = anchorObj . customPropName ;
283
+ let anchorName = anchorObj ? .anchorName ;
284
+ const customPropName = anchorObj ? .customPropName ;
285
285
if ( targetEl && ! anchorName ) {
286
286
const positionAnchorProperty = getCSSPropertyValue (
287
287
targetEl ,
@@ -364,10 +364,10 @@ export async function parseCSS(styleData: StyleData[]) {
364
364
365
365
if ( positionAreaData ) {
366
366
for ( const { selector } of selectors ) {
367
- positionAreas [ selector ] = {
368
- ...positionAreas [ selector ] ,
369
- positionArea : positionAreaData ,
370
- } ;
367
+ positionAreas [ selector ] = [
368
+ ...( positionAreas [ selector ] ?? [ ] ) ,
369
+ positionAreaData ,
370
+ ] ;
371
371
}
372
372
}
373
373
if ( updated || positionAreaData ?. changed ) {
@@ -760,48 +760,34 @@ export async function parseCSS(styleData: StyleData[]) {
760
760
document . querySelectorAll ( targetSel ) ;
761
761
for ( const targetEl of targets ) {
762
762
// 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
+ }
805
791
}
806
792
}
807
793
0 commit comments