Package Exports
- @spectrum-web-components/shared/package.json
- @spectrum-web-components/shared/src/focus-visible.d.ts
- @spectrum-web-components/shared/src/focus-visible.js
- @spectrum-web-components/shared/src/focus-visible.js.map
- @spectrum-web-components/shared/src/focus-visible.ts
- @spectrum-web-components/shared/src/focusable.css
- @spectrum-web-components/shared/src/focusable.css.d.ts
- @spectrum-web-components/shared/src/focusable.css.js
- @spectrum-web-components/shared/src/focusable.css.js.map
- @spectrum-web-components/shared/src/focusable.css.ts
- @spectrum-web-components/shared/src/focusable.d.ts
- @spectrum-web-components/shared/src/focusable.js
- @spectrum-web-components/shared/src/focusable.js.map
- @spectrum-web-components/shared/src/focusable.ts
- @spectrum-web-components/shared/src/get-active-element.d.ts
- @spectrum-web-components/shared/src/get-active-element.js
- @spectrum-web-components/shared/src/get-active-element.js.map
- @spectrum-web-components/shared/src/get-active-element.ts
- @spectrum-web-components/shared/src/index.d.ts
- @spectrum-web-components/shared/src/index.js
- @spectrum-web-components/shared/src/index.js.map
- @spectrum-web-components/shared/src/index.ts
- @spectrum-web-components/shared/src/like-anchor.d.ts
- @spectrum-web-components/shared/src/like-anchor.js
- @spectrum-web-components/shared/src/like-anchor.js.map
- @spectrum-web-components/shared/src/like-anchor.ts
- @spectrum-web-components/shared/src/missing-types.d.ts
- @spectrum-web-components/shared/src/observe-slot-text.d.ts
- @spectrum-web-components/shared/src/observe-slot-text.js
- @spectrum-web-components/shared/src/observe-slot-text.js.map
- @spectrum-web-components/shared/src/observe-slot-text.ts
Readme
Description
Shared mixins, tools, etc. that support developing Spectrum Web Components.
Usage
npm install @spectrum-web-components/sharedIndividual base classes and mixins can be imported as follows:
import {
Focusable,
FocusVisiblePolyfillMixin,
getActiveElement,
LikeAnchor,
ObserveSlotText
} from '@spectrum-web-components/shared';Focusable
The Focusable subclass of LitElement adds some helpers method and lifecycle coverage in order 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 aybolit delegate-focus-mixin at https://github.com/web-padawan/aybolit/blob/master/packages/core/src/mixins/delegate-focus-mixin.js
import { Focusable } from '@spectrum-web-components/shared/lib/focusable';
import { html } from 'lit-element';
class FocusableButton extends Focusable {
public static get styles(): CSSResultArray {
return [...super.styles];
}
public get focusElement(): HTMLElement {
/* istanbul ignore if */
if (!this.shadowRoot) {
return this;
}
return this.shadowRoot.querySelector('#button') as HTMLElement;
}
protected render(): TemplateResult {
return html`
<button
id="button"
>
Focus for this button is being managed by the focusable base class.
</button>
`;
}
}FocusVisiblePolyfillMixin
Use this mixin if you would like to leverage :focus-visible based selectors in your CSS. Learn more about the polyfill that powers this.
getActiveElement
Use this helper to find an activeElement in your component. Learn more about tracking active elements over shadow DOM boundaries.
LikeAnchor
Mix download, label, href, and target properties into your element to allow it to act more like an HTMLAnchorElement.
ObserverSlotText
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 leverages to apply a mutation observe onto your element that tracks characterData mutations so that you can resspond as desired.
import { ObserveSlotText } from '@spectrum-web-components/shared/lib/oberve-slot-text';
import { LitElement, html } from 'lit-element';
class ObserveSlotTextElement extends ObserveSlotText(LitElement, '#observing-slot') {
protected render(): TemplateResult {
return html`
<button
id="button"
>
<slot
id="observing-slot"
@slotchange=${this.manageObservedSlot}
></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);