shared
NPM 1.9.0
Overview
The @spectrum-web-components/shared
package provides essential base classes, mixins, and utilities that support developing Spectrum Web Components. This package contains foundational tools for focus management, slot observation, accessibility enhancements, and other common functionality used across the component library.
Usage
npm install @spectrum-web-components/shared
Individual base classes, mixins, and utilities can be imported as follows:
import { Focusable, FocusVisiblePolyfillMixin, getActiveElement, getDeepElementFromPoint, LikeAnchor, ObserveSlotPresence, ObserveSlotText, } from '@spectrum-web-components/shared';
Utilities
getDeepElementFromPoint
The getDeepElementFromPoint
method allows you to obtain the deepest possible element at a given coordinates on the current page. The method will step into any available shadowRoot
s until it reaches the first element with no shadowRoot
or no children available at the given coordinates.
When to use: Use this when you need to find the actual target element at specific coordinates, especially when working with shadow DOM where document.elementFromPoint()
might not give you the deepest element.
import { getDeepElementFromPoint } from '@spectrum-web-components/shared'; const element = getDeepElementFromPoint(x, y);
getActiveElement
Use this helper to find an activeElement
in your component.
When to use: Use this when you need to determine which element currently has focus, especially in components with shadow DOM where document.activeElement
might not give you the correct element.
import { getActiveElement } from '@spectrum-web-components/shared'; const activeEl = getActiveElement(this);
Base classes
Focusable
The Focusable
subclass of SpectrumElement
adds helper methods and lifecycle coverage to support passing focus to a container element inside of a custom element. The Focusable base class handles tabindex setting into shadowed elements automatically and is based heavily on the
When to use: Use this base class when creating custom elements that need to delegate focus to an internal element (like a button or input) while maintaining proper tabindex management and accessibility.
import { Focusable } from '@spectrum-web-components/shared'; import { html, TemplateResult } from '@spectrum-web-components/base'; class FocusableButton extends Focusable { public get focusElement(): HTMLElement { return this.shadowRoot.querySelector('#button') as HTMLElement; } protected override render(): TemplateResult { return html` <button id="button"> Focus for this button is being managed by the focusable base class. </button> `; } }
Mixins
LikeAnchor
Mix download
, label
, href
, target
, rel
, and referrerpolicy
properties into your element to allow it to act more like an HTMLAnchorElement
. It also provides a renderAnchor
method for rendering anchor elements.
When to use: Use this mixin when creating custom elements that need to behave like links or buttons with link-like functionality, such as action buttons that can navigate to URLs.
import { LikeAnchor } from '@spectrum-web-components/shared'; import { ReactiveElement, html, TemplateResult } from '@spectrum-web-components/base'; class MyLinkElement extends LikeAnchor(ReactiveElement) { protected render(): TemplateResult { return this.renderAnchor({ id: 'my-anchor', className: 'my-link', anchorContent: html`<slot></slot>`, }); } }
FocusVisiblePolyfillMixin
This mixin coordinates with the focus-visible polyfill to ensure proper behavior across browsers.
When to use: Use this mixin when you need to leverage :focus-visible
based selectors in your CSS.
import { FocusVisiblePolyfillMixin } from '@spectrum-web-components/shared'; class MyElement extends FocusVisiblePolyfillMixin(HTMLElement) { // Your element now supports :focus-visible selectors and coordinates with the polyfill }
ObserveSlotPresence
When working with styles that are driven by the conditional presence of <slot>
s in a component's shadow DOM, you will need to track whether light DOM that is meant for that slot exists. Use the ObserveSlotPresence
mixin to target specific light DOM to observe the presence of and trigger this.requestUpdate()
calls when content fulfilling that selector comes in and out of availability.
When to use: Use this mixin when you need to conditionally render UI or apply styles based on whether specific slotted content is present. Common use cases include showing/hiding labels, icons, or wrapper elements.
import { ObserveSlotPresence } from '@spectrum-web-components/shared'; import { ReactiveElement, html, TemplateResult } from '@spectrum-web-components/base'; class ObserveSlotPresenceElement extends ObserveSlotPresence( ReactiveElement, '[slot="conditional-slot"]' ) { // Translate the mixin properties into locally understandable language protected get hasConditionalSlotContent() { return this.slotContentIsPresent; } protected override render(): TemplateResult { return html` <button id="button"> ${this.hasConditionalSlotContent ? html`<slot name="conditional-slot"></slot>` : html`` } </button> `; } protected updated(): void { console.log(this.slotContentIsPresent); // => true when <observing-slot-presence-element><div slot="conditional-slot"></div></observing-slot-presence-element> } } customElements.define('observing-slot-presence-element', ObserveSlotPresenceElement);
ObserveSlotText
When working with <slot>
s and their slotchange
event, you will have the opportunity to capture when the nodes and/or elements in your element are added or removed. However, if the textContent
of a text node changes, you will not receive the slotchange
event because the slot hasn't actually received new nodes and/or elements in the exchange. When working with a lit-html binding <your-element>${text}</your-element>
that means you will not receive a slotchange
event when the value of text
goes from text = ''
to text = 'something'
or the other way. In these cases the ObserveSlotText
can be leveraged to apply a mutation observer onto your element that tracks characterData
mutations so that you can respond as desired.
When to use: Use this mixin when you need to detect changes in text content within slots, especially for dynamic text that changes after the initial render. Useful for components that need to react to text content changes for layout or styling purposes.
import { ObserveSlotText } from '@spectrum-web-components/shared'; import { ReactiveElement, html, TemplateResult } from '@spectrum-web-components/base'; class ObserveSlotTextElement extends ObserveSlotText(ReactiveElement) { protected override render(): TemplateResult { return html` <button id="button"> <slot id="observing-slot" @slotchange=${this.manageTextObservedSlot} ></slot> </button> `; } protected updated(): void { console.log(this.slotHasContent); // => true when <observing-slot-text-element>Text</observing-slot-text-element> } } customElements.define('observing-slot-text-element', ObserveSlotTextElement);
For named slots, you can supply the name of the slot as the second argument:
class ObserveSlotTextElement extends ObserveSlotText(ReactiveElement, 'button-label') { protected override render(): TemplateResult { return html` <button id="button" > <button id="button"> <slot id="observing-slot" @slotchange=${this.manageObservedSlot} @slotchange=${this.manageTextObservedSlot} ></slot> </button> `; } }
Changelog
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.9.0
1.8.0
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.8.0
1.7.0
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.7.0
1.6.0
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.6.0
1.5.0
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.5.0
1.4.0
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.4.0
1.3.0
Patch Changes
- Updated dependencies []:
- @spectrum-web-components/base@1.3.0
All notable changes to this project will be documented in this file. See
1.2.0 (2025-02-27)
Note: Version bump only for package @spectrum-web-components/shared
1.1.2 (2025-02-12)
Note: Version bump only for package @spectrum-web-components/shared
1.1.1 (2025-01-29)
Note: Version bump only for package @spectrum-web-components/shared
1.1.0 (2025-01-29)
Note: Version bump only for package @spectrum-web-components/shared
1.0.1 (2024-11-11)
Note: Version bump only for package @spectrum-web-components/shared
1.0.0 (2024-10-31)
Note: Version bump only for package @spectrum-web-components/shared
0.49.0 (2024-10-15)
Note: Version bump only for package @spectrum-web-components/shared
0.48.1 (2024-10-01)
Note: Version bump only for package @spectrum-web-components/shared
0.48.0 (2024-09-17)
Note: Version bump only for package @spectrum-web-components/shared
0.47.2 (2024-09-03)
Note: Version bump only for package @spectrum-web-components/shared
0.47.1 (2024-08-27)
Note: Version bump only for package @spectrum-web-components/shared
0.47.0 (2024-08-20)
Note: Version bump only for package @spectrum-web-components/shared
0.46.0 (2024-08-08)
Note: Version bump only for package @spectrum-web-components/shared
0.45.0 (2024-07-30)
Note: Version bump only for package @spectrum-web-components/shared
0.44.0 (2024-07-15)
Features
- action-bar: support for action-menus (
#3780 ) (4aff599 )
0.43.0 (2024-06-11)
Note: Version bump only for package @spectrum-web-components/shared
0.42.5 (2024-05-24)
Note: Version bump only for package @spectrum-web-components/shared
0.42.4 (2024-05-14)
Note: Version bump only for package @spectrum-web-components/shared
0.42.3 (2024-05-01)
Note: Version bump only for package @spectrum-web-components/shared
0.42.2 (2024-04-03)
Note: Version bump only for package @spectrum-web-components/shared
0.42.1 (2024-04-02)
Bug Fixes
- shared: ensure the "updateComplete" in Focusable is stable (
885b4a5 )
0.42.0 (2024-03-19)
Note: Version bump only for package @spectrum-web-components/shared
0.41.2 (2024-03-05)
Note: Version bump only for package @spectrum-web-components/shared
0.41.1 (2024-02-22)
Note: Version bump only for package @spectrum-web-components/shared
0.41.0 (2024-02-13)
Bug Fixes
- support generating random IDs outside of secure contexts (
485a67c )
0.40.5 (2024-02-05)
Note: Version bump only for package @spectrum-web-components/shared
0.40.4 (2024-01-29)
Bug Fixes
- shared: update and expand attribute coverage in likeAnchor (
5cb5f1d )
0.40.3 (2024-01-11)
Note: Version bump only for package @spectrum-web-components/shared
0.40.2 (2023-12-18)
Note: Version bump only for package @spectrum-web-components/shared
0.40.1 (2023-12-05)
Note: Version bump only for package @spectrum-web-components/shared
0.40.0 (2023-11-16)
Note: Version bump only for package @spectrum-web-components/shared
0.39.4 (2023-11-02)
Note: Version bump only for package @spectrum-web-components/shared
0.39.3 (2023-10-18)
Note: Version bump only for package @spectrum-web-components/shared
0.39.2 (2023-10-13)
Note: Version bump only for package @spectrum-web-components/shared
0.39.1 (2023-10-06)
Note: Version bump only for package @spectrum-web-components/shared
0.39.0 (2023-09-25)
Bug Fixes
- shared: scope querySelector to direct children, like slots are (
515eaee )
0.38.0 (2023-09-05)
Note: Version bump only for package @spectrum-web-components/shared
0.37.0 (2023-08-18)
Features
- overlay: ship Overlay API v2 (
67b5d1b )
0.36.0 (2023-08-18)
Note: Version bump only for package @spectrum-web-components/shared
0.35.0 (2023-07-31)
Bug Fixes
- meter, progress-bar, progress-circle: use innerText when label is not provided (
#3483 ) (59358c7 )
0.34.0 (2023-07-11)
Note: Version bump only for package @spectrum-web-components/shared
0.33.2 (2023-06-14)
Note: Version bump only for package @spectrum-web-components/shared
0.33.0 (2023-06-08)
Note: Version bump only for package @spectrum-web-components/shared
0.32.0 (2023-06-01)
Note: Version bump only for package @spectrum-web-components/shared
0.31.0 (2023-05-17)
Bug Fixes
- shared: allow "disabled" first to return to "tabindex=0" in "focusable" (
160bc59 )
0.30.0 (2023-05-03)
Note: Version bump only for package @spectrum-web-components/shared
0.15.7 (2023-04-24)
Note: Version bump only for package @spectrum-web-components/shared
0.15.6 (2023-04-05)
Note: Version bump only for package @spectrum-web-components/shared
0.15.5 (2023-01-23)
Note: Version bump only for package @spectrum-web-components/shared
0.15.4 (2023-01-09)
Note: Version bump only for package @spectrum-web-components/shared
0.15.3 (2022-11-21)
Note: Version bump only for package @spectrum-web-components/shared
0.15.2 (2022-11-14)
Note: Version bump only for package @spectrum-web-components/shared
0.15.1 (2022-10-17)
Bug Fixes
- correct the relationship between overlayWillCloseCallback and phased animations (
c63db8d )
0.15.0 (2022-08-09)
Features
- include all Dev Mode files in side effects (
f70817c )
0.14.5 (2022-08-04)
Note: Version bump only for package @spectrum-web-components/shared
0.14.4 (2022-07-18)
Note: Version bump only for package @spectrum-web-components/shared
0.14.3 (2022-06-29)
Note: Version bump only for package @spectrum-web-components/shared
0.14.2 (2022-06-07)
Note: Version bump only for package @spectrum-web-components/shared
0.14.1 (2022-05-12)
Note: Version bump only for package @spectrum-web-components/shared
0.14.0 (2022-04-21)
Features
- conditionally load focus-visible polyfill (
6b5e5cf ) - reparentChildren - insertAdjacentElement style API (
07f966f ) - reparentChildren - refactored arguments - breaking change (
dea2bc5 )
0.13.7 (2022-03-30)
Note: Version bump only for package @spectrum-web-components/shared
0.13.6 (2022-03-08)
Note: Version bump only for package @spectrum-web-components/shared
0.13.5 (2022-03-04)
Note: Version bump only for package @spectrum-web-components/shared
0.13.4 (2022-02-22)
Bug Fixes
- dialog: updates for delivering dialog content accessibly (
f0ed33c )
0.13.3 (2022-01-26)
Note: Version bump only for package @spectrum-web-components/shared
0.13.2 (2022-01-07)
Note: Version bump only for package @spectrum-web-components/shared
0.13.1 (2021-12-13)
Note: Version bump only for package @spectrum-web-components/shared
0.13.0 (2021-11-08)
Features
- update lit-* dependencies, wip (
377f3c8 )
0.12.11 (2021-11-08)
Note: Version bump only for package @spectrum-web-components/shared
0.12.10 (2021-11-02)
Bug Fixes
- centralize updated first focusable selector (
300e84c )
0.12.9 (2021-10-12)
Bug Fixes
- add likeAnchor API to Card element (
5c338fb )
0.12.8 (2021-09-13)
Bug Fixes
- ensure lazily loaded focusElements do not crash (
64f2a54 )
0.12.7 (2021-07-22)
Note: Version bump only for package @spectrum-web-components/shared
0.12.6 (2021-07-01)
Bug Fixes
- allow detached elements to be used as content for an overlay (
3ad8383 ) - manage "lang" via context provided by "sp-theme" (
b1e3457 )
0.12.5 (2021-06-16)
Note: Version bump only for package @spectrum-web-components/shared
0.12.4 (2021-05-24)
Bug Fixes
- no scroll update when managing elements outside of the tab order (
144c548 ) - prevent tabindex=-1 elements from placing focus on their host (
1ac1293 )
0.12.3 (2021-05-12)
Bug Fixes
- allow rendered anchors to be aria-hidden (
2e9aa23 ) - update "reparentChildren" types for flexibility (
2d358be )
0.12.2 (2021-04-09)
Note: Version bump only for package @spectrum-web-components/shared
0.12.1 (2021-03-29)
Note: Version bump only for package @spectrum-web-components/shared
0.12.0 (2021-03-22)
Bug Fixes
- renamed the file but not this (
ad94e53 ) - split-button tests & lots of cleanup based on review feedback (
10b4a04 ), closes#1189
Features
- picker: process field-label content for more accurate a11y tree (
dc9df54 ) - deprecate sp-menu in PickerBase derived classes (
bbb773c )
0.11.1 (2021-03-05)
Note: Version bump only for package @spectrum-web-components/shared
0.11.0 (2021-03-04)
Features
- use latest exports specification (
a7ecf4b )
0.10.0 (2021-02-11)
Bug Fixes
- update to latest spectrum-css packages (
a5ca19f )
Features
- allow activation of longpress content (
55e71fd )
0.9.0 (2021-01-21)
Bug Fixes
- button: relate to this.href correctly (
fade3ea ) - include the "types" entry in package.json files (
b432f59 ) - use latest @spectrum-css/* versions (
c35eb86 )
Features
- button: use synthetic button instead of native (
49e94bc ) - shared: conditionally apply focus-visible polyfill (
b854df6 )
Performance Improvements
- shared: focus-visible, lazily instantiate the focus-visible polyfilling (
fe257c1 )
0.8.0 (2021-01-13)
Bug Fixes
- button: relate to this.href correctly (
fade3ea ) - include the "types" entry in package.json files (
b432f59 ) - use latest @spectrum-css/* versions (
c35eb86 )
Features
- button: use synthetic button instead of native (
49e94bc ) - shared: conditionally apply focus-visible polyfill (
b854df6 )
Performance Improvements
- shared: focus-visible, lazily instantiate the focus-visible polyfilling (
fe257c1 )
0.7.4 (2020-10-12)
Note: Version bump only for package @spectrum-web-components/shared
0.7.3 (2020-10-12)
Bug Fixes
- include default export in the "exports" fields (
f32407d )
0.7.2 (2020-09-25)
Bug Fixes
- update side effect listings (
8160d3a )
0.7.1 (2020-09-14)
Bug Fixes
- top-nav: prototype top-nav pattern (
9708f6f ) - flatten assigned nodes to observe text of nested slots (
08ffd68 )
0.7.0 (2020-08-31)
Bug Fixes
- split-button: follow visible tab order (
966d3b6 )
Features
- split-button: add split-button pattern (
4833a59 ) - use 3.0.0-beta.* release for styles (
877b485 )
0.6.0 (2020-07-27)
Bug Fixes
- ensure browser understandable extensions (
f4e59f7 )
Features
- link: support rel attribute (
df4b5a8 )
0.5.1 (2020-07-22)
Bug Fixes
- shared: prevent focusable returning focus to host (
745f7b0 ) - tabs: correct entry focus element (
64407d3 )
0.5.0 (2020-07-17)
Features
- leverage "exports" field in package.json (
321abd7 )
0.4.7 (2020-06-08)
Note: Version bump only for package @spectrum-web-components/shared
0.4.6 (2020-04-16)
Performance Improvements
- use "sideEffects" listing in package.json (
7271614 )
0.4.5 (2020-04-10)
Note: Version bump only for package @spectrum-web-components/shared
0.4.4 (2020-04-07)
Note: Version bump only for package @spectrum-web-components/shared
0.4.3 (2020-02-05)
Note: Version bump only for package @spectrum-web-components/shared
0.4.2 (2020-02-01)
Bug Fixes
- shared: fixes search input - fixes
#463 (6833944 ) - shared: removes mousedown event handling in focusable (
4e90d4c )
0.4.1 (2020-01-30)
Bug Fixes
- shared: prevent focusing focusable root on second click (
0fb5006 )
0.4.0 (2020-01-06)
Features
- sidenav: add keyboard accessibility (
6ff622b ) - tab-list: autofocus, :before/after processing, visual test (
83dddb0 )
0.3.3 (2019-12-12)
Bug Fixes
- apply Focuable styles in class extensions (
38f7afd )
0.3.2 (2019-12-02)
Bug Fixes
- shared: include an actual entry point for bundlephobia (
00fd6ab ) - normalize "event" and "error" argument names (
8d382cd )
0.3.1 (2019-11-27)
Bug Fixes
- include "type" in package.json, generate custom-elements.json (
1a8d716 )
0.3.0 (2019-11-19)
Bug Fixes
- shared: fixes focus-visible types in test (
0dc7d68 ) - shared: improves types for focus-visible (
b980f2a )
Features
- add screenshot regression testing to CI (
8205dfe ) - use :focus-visable (via polyfill) instead of :focus (
11c6fc7 )
0.2.0 (2019-11-01)
Bug Fixes
- shared: make Focusable pass disabled always (
a339d6f ) - shared: quiet the angry soul of the explicit any linter (
c278263 )
Features
- shared: add mixing for observing text content changes in a slot (
1318150 )
0.1.4 (2019-10-14)
Performance Improvements
- use imported TypeScript helpers instead of inlining them (
cc2bd0a )
0.1.3 (2019-10-03)
Note: Version bump only for package @spectrum-web-components/shared