generated from oddbird/polyfill-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from oddbird/position-anchor
Position anchor
- Loading branch information
Showing
16 changed files
with
361 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#position-anchor #my-position-target-b { | ||
position-anchor: --my-position-anchor-b; | ||
} | ||
|
||
#position-anchor .target { | ||
position: absolute; | ||
bottom: anchor(top); | ||
left: anchor(right); | ||
position-anchor: --my-position-anchor-a; | ||
} | ||
|
||
#my-position-anchor-a { | ||
anchor-name: --my-position-anchor-a; | ||
margin-bottom: 3em; | ||
} | ||
|
||
#my-position-anchor-b { | ||
anchor-name: --my-position-anchor-b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as csstree from 'css-tree'; | ||
|
||
import { isPositionAnchorDeclaration } from './parse.js'; | ||
import { | ||
generateCSS, | ||
getAST, | ||
getDeclarationValue, | ||
POSITION_ANCHOR_PROPERTY, | ||
type StyleData, | ||
} from './utils.js'; | ||
|
||
// Move `position-anchor` declaration to cascadable `--position-anchor` | ||
// property. | ||
function shiftPositionAnchorData(node: csstree.CssNode, block?: csstree.Block) { | ||
if (isPositionAnchorDeclaration(node) && block) { | ||
block.children.appendData({ | ||
type: 'Declaration', | ||
important: false, | ||
property: POSITION_ANCHOR_PROPERTY, | ||
value: { | ||
type: 'Raw', | ||
value: getDeclarationValue(node), | ||
}, | ||
}); | ||
return { updated: true }; | ||
} | ||
return {}; | ||
} | ||
|
||
export async function cascadeCSS(styleData: StyleData[]) { | ||
for (const styleObj of styleData) { | ||
let changed = false; | ||
const ast = getAST(styleObj.css); | ||
csstree.walk(ast, { | ||
visit: 'Declaration', | ||
enter(node) { | ||
const block = this.rule?.block; | ||
const { updated } = shiftPositionAnchorData(node, block); | ||
if (updated) { | ||
changed = true; | ||
} | ||
}, | ||
}); | ||
|
||
if (changed) { | ||
// Update CSS | ||
styleObj.css = generateCSS(ast); | ||
styleObj.changed = true; | ||
} | ||
} | ||
return styleData.some((styleObj) => styleObj.changed === true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.