Skip to content

Commit 038488e

Browse files
committed
docs(overlay): add instructions on styling overlay content
1 parent 8acfee4 commit 038488e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

documentation/src/components/component.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,24 @@ class ComponentElement extends RouteComponent {
309309
}
310310
}
311311
customElements.define('docs-component', ComponentElement);
312+
313+
class StyledElement extends HTMLElement {
314+
constructor() {
315+
super();
316+
this.attachShadow({ mode: 'open' });
317+
(this.shadowRoot as ShadowRoot).innerHTML = `
318+
<style>
319+
:host {
320+
display: block;
321+
background-color: var(--spectrum-global-color-gray-50);
322+
color: var(--spectrum-global-color-gray-800);
323+
border: 1px solid;
324+
padding: 2em;
325+
}
326+
</style>
327+
<slot></slot>
328+
`;
329+
}
330+
}
331+
332+
customElements.define('styled-element', StyledElement);

packages/overlay/overlay-trigger.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,79 @@ The delivery of hover content can be customized via the `placement` attribute. H
123123
</overlay-trigger>
124124
```
125125

126+
## Styling
127+
128+
Content that is thrown into an overlay (e.g. `[slot="*-content"]`) will be moved out of the CSS scope created by its parent `<overlay-trigger>` and other related elements within the same DOM/shadow tree. In this way styling rules that rely on that scope will not travel with the content into its overlay context. In order to ensure that your overlay content is styled as you would like you will need to bind those styles by applying them inline, including a `<style>` element _inside_ of your content element, or by making your content its own custom element.
129+
130+
**Inline Styles**
131+
132+
```html
133+
<overlay-trigger placement="top-start">
134+
<button slot="trigger">Trigger Element</button>
135+
<div
136+
slot="click-content"
137+
style="
138+
background-color: var(--spectrum-global-color-gray-50);
139+
color: var(--spectrum-global-color-gray-800);
140+
border: 1px solid;
141+
padding: 2em;
142+
"
143+
>
144+
This content is delivered with inline styles.
145+
</div>
146+
</overlay-trigger>
147+
```
148+
149+
**Style Element**
150+
151+
```html
152+
<overlay-trigger placement="top-start">
153+
<button slot="trigger">Trigger Element</button>
154+
<div slot="click-content" id="styled-content">
155+
<style>
156+
#styled-content {
157+
background-color: var(--spectrum-global-color-gray-50);
158+
color: var(--spectrum-global-color-gray-800);
159+
border: 1px solid;
160+
padding: 2em;
161+
}
162+
</style>
163+
This content is delivered with a style element defining its styles.
164+
</div>
165+
</overlay-trigger>
166+
```
167+
168+
**Custom Element**
169+
170+
```html
171+
<overlay-trigger placement="top-start">
172+
<button slot="trigger">Trigger Element</button>
173+
<styled-element slot="click-content">
174+
This content is delivered as its own custom element.
175+
</styled-element>
176+
</overlay-trigger>
177+
<script>
178+
class StyledElement extends HTMLElement {
179+
constructor() {
180+
super();
181+
this.attachShadow({ mode: 'open' });
182+
this.shadowRoot.innerHTML = `
183+
<style>
184+
:host {
185+
background-color: var(--spectrum-global-color-gray-50);
186+
color: var(--spectrum-global-color-gray-800);
187+
border: 1px solid;
188+
padding: 2em;
189+
}
190+
</style>
191+
<slot></slot>
192+
`;
193+
}
194+
}
195+
customElements.define('styled-element', StyledElement);
196+
</script>
197+
```
198+
126199
## Accessibility
127200

128201
When using an `<overlay-trigger>` element, it is important to be sure the that content you project into `slot="trigger"` is "interactive". This means that an element within that branch of DOM will be able to receive focus, and said element will appropriately convert keyboard interactions to `click` events, similar to what you'd find with `<a href="#">Anchors</a>`, `<button>Buttons</button>`, etc. You can find further reading on the subject of accessible keyboard interactions at [https://www.w3.org/WAI/WCAG21/Understanding/keyboard](https://www.w3.org/WAI/WCAG21/Understanding/keyboard).

0 commit comments

Comments
 (0)