-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for styles with a
shady-unscoped
attribute
Since `/deep/` has been removed from Shady DOM v0, users must share styles between elements via style modules (e.g. `<style include="shared-style">`). Under ShadyDOM, these styles are scoped and duplicated between elements. If these styles are intended to replace global styling done with `html /deep/ ...` this is inefficient since the styles are needlessly copied to each element style. Therefore, this adds support for a `shady-unscoped` attribute. If this attribute is placed on a style and native Shadow DOM is not in use, then the style is copied once to the document and are left unscoped. Please note, css custom properties are not supported in `shady-unscoped` styles.
- Loading branch information
Steven Orvell
committed
Oct 18, 2017
1 parent
40058ae
commit 90697bf
Showing
5 changed files
with
181 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.import { | ||
border: 2px solid yellow; | ||
} |
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,26 @@ | ||
<dom-module id="global-shared1"> | ||
<link rel="import" type="css" shady-unscoped href="shady-unscoped-style-import-css.html"> | ||
<template> | ||
<style shady-unscoped> | ||
:root { | ||
--zug: margin: 10px; | ||
} | ||
|
||
.happy { | ||
@apply --zug; | ||
border: 1px solid green; | ||
} | ||
</style> | ||
|
||
<style> | ||
.normal { | ||
border: 3px solid orange; | ||
} | ||
</style> | ||
</template> | ||
</dom-module> | ||
|
||
<dom-module id="global-shared2"> | ||
<link rel="import" type="css" shady-unscoped href="shady-unscoped-style-import-css.html"> | ||
</dom-module> | ||
|
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,121 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script src="../../../web-component-tester/browser.js"></script> | ||
<link rel="import" href="../../polymer.html"> | ||
<link rel="import" href="shady-unscoped-style-import.html"> | ||
</head> | ||
<body> | ||
|
||
<custom-style> | ||
<style is="custom-style"> | ||
html { | ||
--foo: { | ||
padding: 10px; | ||
} | ||
} | ||
</style> | ||
</custom-style> | ||
|
||
<dom-module id="my-element"> | ||
|
||
<template> | ||
<style include="global-shared1 global-shared2"> | ||
:host { | ||
display: block; | ||
} | ||
|
||
.happy { | ||
@apply --foo; | ||
} | ||
</style> | ||
<div id="fromStyle" class="happy">Happy: green</div> | ||
<div id="fromImport" class="import">Happy: yellow</div> | ||
<div id="normal" class="normal">Happy: orange</div> | ||
</template> | ||
|
||
<script> | ||
HTMLImports.whenReady(function() { | ||
Polymer({ | ||
is: 'my-element' | ||
}); | ||
}); | ||
</script> | ||
|
||
</dom-module> | ||
|
||
<dom-module id="my-element2"> | ||
|
||
<template> | ||
<style include="global-shared1"> | ||
:host { | ||
display: block; | ||
} | ||
|
||
</style> | ||
<div id="fromStyle" class="happy">Happy: green</div> | ||
<div id="fromImport" class="import">Happy: yellow</div> | ||
<div id="normal" class="normal">Happy: orange</div> | ||
</template> | ||
|
||
<script> | ||
HTMLImports.whenReady(function() { | ||
Polymer({ is: 'my-element2'}); | ||
}); | ||
</script> | ||
|
||
</dom-module> | ||
|
||
<my-element></my-element> | ||
<my-element2></my-element2> | ||
|
||
<script> | ||
suite('shady-unscoped styles', function() { | ||
|
||
function assertComputed(element, value, property, pseudo) { | ||
var computed = getComputedStyle(element, pseudo); | ||
property = property || 'border-top-width'; | ||
if (Array.isArray(value)) { | ||
assert.oneOf(computed[property], value, 'computed style incorrect for ' + property); | ||
} else { | ||
assert.equal(computed[property], value, 'computed style incorrect for ' + property); | ||
} | ||
} | ||
|
||
var el1 = document.querySelector('my-element'); | ||
var el2 = document.querySelector('my-element2'); | ||
|
||
test('unscoped styles apply', function() { | ||
assertComputed(el1.$.fromStyle, '1px'); | ||
assertComputed(el1.$.fromImport, '2px'); | ||
assertComputed(el2.$.fromStyle, '1px'); | ||
assertComputed(el2.$.fromImport, '2px'); | ||
}); | ||
|
||
test('shared and @apply apply when used with unscoped styles', function() { | ||
assertComputed(el1.$.fromStyle, '10px', 'padding'); | ||
assertComputed(el1.$.normal, '3px'); | ||
assertComputed(el2.$.normal, '3px'); | ||
}) | ||
|
||
test('unscoped styling de-duped in ShadyDOM', function() { | ||
if (Polymer.Settings.useNativeShadow) { | ||
this.skip(); | ||
} | ||
assert.equal(document.head.querySelectorAll('style[shady-unscoped]').length, 2); | ||
}); | ||
|
||
test('@apply does not apply under ShadyDOM for shady-unscoped styles', function() { | ||
if (Polymer.Settings.useNativeShadow) { | ||
this.skip(); | ||
} | ||
assertComputed(el1.$.fromStyle, '0px', 'margin'); | ||
assertComputed(el2.$.fromStyle, '0px', 'margin'); | ||
}) | ||
|
||
|
||
}); | ||
</script> | ||
</body> | ||
</html> |