Skip to content

Commit bd6ea70

Browse files
authored
feat: add SlotStylesMixin for injecting styles (#2445)
1 parent 836dde9 commit bd6ea70

5 files changed

Lines changed: 179 additions & 6 deletions

File tree

‎packages/field-base/index.d.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { ActiveMixin } from './src/active-mixin.js';
12
export { AriaLabelMixin } from './src/aria-label-mixin.js';
23
export { CharLengthMixin } from './src/char-length-mixin.js';
34
export { ClearButtonMixin } from './src/clear-button-mixin.js';
@@ -10,12 +11,12 @@ export { HelperTextMixin } from './src/helper-text-mixin.js';
1011
export { InputFieldMixin } from './src/input-field-mixin.js';
1112
export { InputMixin } from './src/input-mixin.js';
1213
export { InputSlotMixin } from './src/input-slot-mixin.js';
14+
export { KeyboardMixin } from './src/keyboard-mixin.js';
1315
export { LabelMixin } from './src/label-mixin.js';
1416
export { PatternMixin } from './src/pattern-mixin.js';
1517
export { SlotMixin } from './src/slot-mixin.js';
18+
export { SlotStylesMixin } from './src/slot-styles-mixin.js';
19+
export { TabindexMixin } from './src/tabindex-mixin.js';
1620
export { TextAreaSlotMixin } from './src/text-area-slot-mixin.js';
1721
export { TextFieldMixin } from './src/text-field-mixin.js';
1822
export { ValidateMixin } from './src/validate-mixin.js';
19-
export { TabindexMixin } from './src/tabindex-mixin.js';
20-
export { ActiveMixin } from './src/active-mixin.js';
21-
export { KeyboardMixin } from './src/keyboard-mixin.js';

‎packages/field-base/index.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { ActiveMixin } from './src/active-mixin.js';
12
export { AriaLabelMixin } from './src/aria-label-mixin.js';
23
export { CharLengthMixin } from './src/char-length-mixin.js';
34
export { ClearButtonMixin } from './src/clear-button-mixin.js';
@@ -10,12 +11,12 @@ export { HelperTextMixin } from './src/helper-text-mixin.js';
1011
export { InputFieldMixin } from './src/input-field-mixin.js';
1112
export { InputMixin } from './src/input-mixin.js';
1213
export { InputSlotMixin } from './src/input-slot-mixin.js';
14+
export { KeyboardMixin } from './src/keyboard-mixin.js';
1315
export { LabelMixin } from './src/label-mixin.js';
1416
export { PatternMixin } from './src/pattern-mixin.js';
1517
export { SlotMixin } from './src/slot-mixin.js';
18+
export { SlotStylesMixin } from './src/slot-styles-mixin.js';
19+
export { TabindexMixin } from './src/tabindex-mixin.js';
1620
export { TextAreaSlotMixin } from './src/text-area-slot-mixin.js';
1721
export { TextFieldMixin } from './src/text-field-mixin.js';
1822
export { ValidateMixin } from './src/validate-mixin.js';
19-
export { TabindexMixin } from './src/tabindex-mixin.js';
20-
export { ActiveMixin } from './src/active-mixin.js';
21-
export { KeyboardMixin } from './src/keyboard-mixin.js';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2021 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
/**
8+
* Mixin to insert styles into the outer scope to handle slotted components.
9+
* This is useful e.g. to hide native `<input type="number">` controls.
10+
*/
11+
declare function SlotStylesMixin<T extends new (...args: any[]) => {}>(base: T): T & SlotStylesMixinConstructor;
12+
13+
interface SlotStylesMixinConstructor {
14+
new (...args: any[]): SlotStylesMixin;
15+
}
16+
17+
interface SlotStylesMixin {
18+
/**
19+
* List of styles to insert into root.
20+
*/
21+
readonly slotStyles: string[];
22+
}
23+
24+
export { SlotStylesMixinConstructor, SlotStylesMixin };
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
2+
3+
const stylesMap = new WeakMap();
4+
5+
/**
6+
* Get all the styles inserted into root.
7+
* @param {DocumentOrShadowRoot} root
8+
* @return {Set<string>}
9+
*/
10+
function getRootStyles(root) {
11+
if (!stylesMap.has(root)) {
12+
stylesMap.set(root, new Set());
13+
}
14+
15+
return stylesMap.get(root);
16+
}
17+
18+
/**
19+
* Insert styles into the root.
20+
* @param {string} styles
21+
* @param {DocumentOrShadowRoot} root
22+
*/
23+
function insertStyles(styles, root) {
24+
const style = document.createElement('style');
25+
style.textContent = styles;
26+
27+
if (root === document) {
28+
document.head.appendChild(style);
29+
} else {
30+
root.insertBefore(style, root.firstChild);
31+
}
32+
}
33+
34+
const SlotStylesMixinImplementation = (superclass) =>
35+
class SlotStylesMixinClass extends superclass {
36+
/**
37+
* List of styles to insert into root.
38+
* @protected
39+
*/
40+
get slotStyles() {
41+
return {};
42+
}
43+
44+
/** @protected */
45+
connectedCallback() {
46+
super.connectedCallback();
47+
48+
this.__applySlotStyles();
49+
}
50+
51+
/** @private */
52+
__applySlotStyles() {
53+
const root = this.getRootNode();
54+
const rootStyles = getRootStyles(root);
55+
56+
this.slotStyles.forEach((styles) => {
57+
if (!rootStyles.has(styles)) {
58+
insertStyles(styles, root);
59+
rootStyles.add(styles);
60+
}
61+
});
62+
}
63+
};
64+
65+
/**
66+
* Mixin to insert styles into the outer scope to handle slotted components.
67+
* This is useful e.g. to hide native `<input type="number">` controls.
68+
*/
69+
export const SlotStylesMixin = dedupingMixin(SlotStylesMixinImplementation);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync } from '@vaadin/testing-helpers';
3+
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
4+
import { SlotMixin } from '../src/slot-mixin.js';
5+
import { SlotStylesMixin } from '../src/slot-styles-mixin.js';
6+
7+
describe('slot-styles-mixin', () => {
8+
const COLOR = 'rgb(0, 100, 0)';
9+
let wrapper, element, button;
10+
11+
before(() => {
12+
customElements.define(
13+
'slot-styles-mixin-element',
14+
class extends SlotStylesMixin(SlotMixin(PolymerElement)) {
15+
static get template() {
16+
return html`<slot name="button"></slot>`;
17+
}
18+
19+
get slots() {
20+
return {
21+
button: () => {
22+
const button = document.createElement('button');
23+
button.textContent = 'Button';
24+
return button;
25+
}
26+
};
27+
}
28+
29+
get slotStyles() {
30+
return [
31+
`
32+
button[slot='button'] {
33+
color: ${COLOR};
34+
}
35+
`
36+
];
37+
}
38+
}
39+
);
40+
});
41+
42+
beforeEach(() => {
43+
wrapper = fixtureSync('<div></div');
44+
wrapper.attachShadow({ mode: 'open' });
45+
element = document.createElement('slot-styles-mixin-element');
46+
wrapper.shadowRoot.appendChild(element);
47+
button = element.querySelector('button');
48+
});
49+
50+
afterEach(() => {
51+
element.remove();
52+
});
53+
54+
it('should append styles to wrapper shadow root', () => {
55+
expect(getComputedStyle(button).color).to.equal(COLOR);
56+
});
57+
58+
it('should only append styles with same ID once', () => {
59+
const sibling = document.createElement('slot-styles-mixin-element');
60+
element.parentNode.appendChild(sibling);
61+
expect(wrapper.shadowRoot.querySelectorAll('style').length).to.equal(1);
62+
});
63+
64+
it('should append styles when moved to other shadow root', () => {
65+
const inner = document.createElement('div');
66+
inner.attachShadow({ mode: 'open' });
67+
wrapper.shadowRoot.appendChild(inner);
68+
// move to inner shadow root
69+
inner.shadowRoot.appendChild(element);
70+
expect(getComputedStyle(button).color).to.equal(COLOR);
71+
});
72+
73+
it('should append styles when moved to document body', () => {
74+
// move out of shadow root
75+
wrapper.appendChild(element);
76+
expect(getComputedStyle(button).color).to.equal(COLOR);
77+
});
78+
});

0 commit comments

Comments
 (0)