inclusive-elements 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  **Accessible, lightweight, unstyled implementations of common UI controls.**
4
4
 
5
- [**Demo**](https://tobyzerner.github.io/inclusive-elements/demo/index.html)
5
+ [**Demo**](https://tobyzerner.github.io/inclusive-elements/index.html)
6
6
 
7
7
  Basic interactive UI patterns like popup menus, tooltips, and modals are surprisingly hard to get right on the web. Component libraries tend to be weighty, opinionated, and come tightly coupled with styles you don't want. On the other hand, rolling your own components feels like you're reinventing the wheel and puts accessibility at risk.
8
8
 
package/package.json CHANGED
@@ -1,41 +1,35 @@
1
1
  {
2
2
  "name": "inclusive-elements",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Accessible, lightweight, unstyled implementations of common UI controls.",
5
- "main": "dist/index.js",
6
- "module": "dist/index.js",
5
+ "module": "./dist/inclusive-elements.js",
7
6
  "type": "module",
8
- "types": "dist/index.d.ts",
7
+ "types": "./dist/types/index.d.ts",
9
8
  "license": "MIT",
10
9
  "repository": "tobyzerner/inclusive-elements",
11
10
  "files": [
12
- "dist",
13
- "README.md"
11
+ "./dist",
12
+ "./README.md"
14
13
  ],
15
14
  "sideEffects": false,
16
15
  "scripts": {
17
- "build": "tsc && rollup -c && npm run build:demo",
18
- "build:watch": "rollup -cw",
19
- "build:demo": "rollup -c demo/rollup.config.js",
16
+ "dev": "vite",
17
+ "build": "tsc --noEmit && vite build && tsc --emitDeclarationOnly",
18
+ "preview": "vite preview",
20
19
  "release": "release-it --npm.tag=latest"
21
20
  },
22
21
  "dependencies": {
23
- "@floating-ui/dom": "^0.5.2",
22
+ "@floating-ui/dom": "^1.0.0",
24
23
  "focus-trap": "^6.7.2",
25
24
  "hello-goodbye": "^0.1.0-beta.5",
26
25
  "tabbable": "^5.2.1"
27
26
  },
28
27
  "devDependencies": {
29
28
  "@release-it/keep-a-changelog": "^2.3.0",
30
- "@rollup/plugin-node-resolve": "^11.2.1",
31
- "@rollup/plugin-typescript": "^8.3.0",
32
29
  "prettier": "^2.6.2",
33
30
  "release-it": "^14.12.4",
34
- "rollup": "^2.66.1",
35
- "rollup-plugin-filesize": "^9.1.2",
36
- "rollup-plugin-terser": "^7.0.2",
37
- "tslib": "^2.3.1",
38
- "typescript": "^4.5.5"
31
+ "typescript": "^4.5.5",
32
+ "vite": "^3.0.4"
39
33
  },
40
34
  "release-it": {
41
35
  "github": {
@@ -1,17 +0,0 @@
1
- export declare type AlertOptions = {
2
- key?: string;
3
- duration?: number;
4
- };
5
- export default class AlertsElement extends HTMLElement {
6
- static duration: number;
7
- private timeouts;
8
- private index;
9
- connectedCallback(): void;
10
- show(el: HTMLElement, options?: AlertOptions): string;
11
- dismiss(el: HTMLElement): void;
12
- dismiss(key: string): void;
13
- clear(): void;
14
- speak(message: string): void;
15
- private startTimeout;
16
- private clearTimeout;
17
- }
package/dist/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export { default as AlertsElement } from './alerts/alerts';
2
- export { default as MenuElement } from './menu/menu';
3
- export { default as ModalElement } from './modal/modal';
4
- export { default as PopupElement } from './popup/popup';
5
- export { default as ToolbarElement } from './toolbar/toolbar';
6
- export { default as TooltipElement } from './tooltip/tooltip';
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- import{move as t,hello as e,goodbye as i,cancel as s}from"hello-goodbye";import{isFocusable as n,focusable as o}from"tabbable";import{createFocusTrap as a}from"focus-trap";import{autoUpdate as r,computePosition as h,shift as d,flip as c,size as l}from"@floating-ui/dom";class u extends HTMLElement{static duration=1e4;timeouts=new Map;index=0;connectedCallback(){this.hasAttribute("role")||this.setAttribute("role","status"),this.hasAttribute("aria-live")||this.setAttribute("aria-live","polite"),this.hasAttribute("aria-relevant")||this.setAttribute("aria-relevant","additions")}show(i,s={}){const n=s.key||String(this.index++);this.dismiss(n),i.dataset.key=n,t(this.children,(()=>{this.append(i),e(i)}));const o=void 0!==s.duration?Number(s.duration):u.duration;return o>0&&(this.startTimeout(i,o),i.addEventListener("mouseenter",this.clearTimeout.bind(this,i)),i.addEventListener("focusin",this.clearTimeout.bind(this,i)),i.addEventListener("mouseleave",this.startTimeout.bind(this,i,o)),i.addEventListener("focusout",this.startTimeout.bind(this,i,o))),n}dismiss(e){"string"!=typeof e?(t(this.children,(()=>{i(e,{finish:()=>this.removeChild(e)})})),this.clearTimeout(e)):this.querySelectorAll(`[data-key="${e}"]`).forEach((t=>{this.dismiss(t)}))}clear(){for(const t of this.children)this.dismiss(t)}speak(t){const e=document.createElement("div");Object.assign(e.style,{clip:"rect(0 0 0 0)",clipPath:"inset(50%)",height:"1px",overflow:"hidden",position:"absolute",whiteSpace:"nowrap",width:"1px"}),e.textContent=t,this.show(e)}startTimeout(t,e){this.clearTimeout(t),this.timeouts.set(t,window.setTimeout((()=>{this.dismiss(t)}),e))}clearTimeout(t){this.timeouts.has(t)&&clearTimeout(this.timeouts.get(t))}}class p extends HTMLElement{static searchDelay=800;search="";searchTimeout;connectedCallback(){this.hasAttribute("role")||this.setAttribute("role","menu"),this.hasAttribute("tabindex")||this.setAttribute("tabindex","-1"),this.items.forEach((t=>{t.hasAttribute("tabindex")||t.setAttribute("tabindex","-1")})),this.addEventListener("keydown",this.onKeyDown)}disconnectedCallback(){this.removeEventListener("keydown",this.onKeyDown)}get items(){return Array.from(this.querySelectorAll("[role^=menuitem]")).filter((t=>n(t)))}onKeyDown=t=>{if(!this.hidden)return"ArrowUp"===t.key?(this.navigate(-1),void t.preventDefault()):"ArrowDown"===t.key?(this.navigate(1),void t.preventDefault()):void(t.key.length>1||t.ctrlKey||t.metaKey||t.altKey||(this.search+=t.key.toLowerCase(),t.preventDefault(),clearTimeout(this.searchTimeout),this.searchTimeout=window.setTimeout((()=>{this.search=""}),p.searchDelay),this.items.some((t=>{if(0===t.textContent?.trim().toLowerCase().indexOf(this.search))return t.focus(),!0}))))};navigate(t){const e=this.items;let i=(document.activeElement instanceof HTMLElement?e.indexOf(document.activeElement):-1)+t;i<0&&(i=e.length-1),i>=e.length&&(i=0),e[i]?.focus()}}class m extends HTMLElement{static attention=t=>t.animate([{transform:"scale(1)"},{transform:"scale(1.1)"},{transform:"scale(1)"}],300);static get observedAttributes(){return["open"]}focusTrap;connected=!1;constructor(){super();const t=document.createElement("template");t.innerHTML='\n <div part="backdrop" style="position: fixed; top: 0; left: 0; right: 0; bottom: 0"></div>\n <div part="content" style="z-index: 1"><slot></slot></div>\n ';const e=this.attachShadow({mode:"open"});e.appendChild(t.content.cloneNode(!0)),this.backdrop.addEventListener("click",(()=>{this.hasAttribute("static")?m.attention?.(e.children[1]):this.close()})),this.focusTrap=a(this,{escapeDeactivates:!1,allowOutsideClick:!0})}connectedCallback(){this.connected=!0,this.content?.hasAttribute("role")||this.content?.setAttribute("role","dialog"),this.content?.hasAttribute("aria-modal")||this.content?.setAttribute("aria-modal","true"),this.content?.hasAttribute("tabindex")||this.content?.setAttribute("tabindex","-1"),this.addEventListener("keydown",(t=>{"Escape"===t.key&&this.open&&(t.preventDefault(),t.stopPropagation(),this.close())})),this.open&&this.wasOpened()}disconnectedCallback(){this.connected=!1}get open(){return this.hasAttribute("open")}set open(t){t?this.setAttribute("open",""):this.removeAttribute("open")}close(){if(!this.open)return;const t=new Event("beforeclose",{cancelable:!0});this.dispatchEvent(t)&&(this.open=!1)}attributeChangedCallback(t,e,i){"open"===t&&this.connected&&(null!==i?this.wasOpened():this.wasClosed())}wasOpened(){this.hidden=!1,e(this),this.focusTrap.activate(),this.querySelector("[autofocus]")?.focus(),this.dispatchEvent(new Event("open"))}wasClosed(){this.focusTrap.deactivate(),i(this,{finish:()=>this.hidden=!0}),this.dispatchEvent(new Event("close"))}get backdrop(){return this.shadowRoot?.firstElementChild}get content(){return this.firstElementChild}}class b extends HTMLElement{cleanup;static get observedAttributes(){return["open"]}constructor(){super();const t=document.createElement("template");t.innerHTML='\n <div part="backdrop" hidden style="position: fixed; top: 0; left: 0; right: 0; bottom: 0"></div>\n <slot></slot>\n ';this.attachShadow({mode:"open"}).appendChild(t.content.cloneNode(!0)),this.backdrop.onclick=()=>this.open=!1}connectedCallback(){this.backdrop.hidden=!0,this.content.hidden=!0,this.open=!1,this.button.setAttribute("aria-expanded","false"),setTimeout((()=>{"menu"===this.content.getAttribute("role")&&this.button.setAttribute("aria-haspopup","true")})),this.button.addEventListener("click",(()=>{this.open=!this.open})),this.button.addEventListener("keydown",(t=>{"ArrowDown"===t.key&&(t.preventDefault(),this.open=!0,o(this.content)[0]?.focus())})),this.addEventListener("keydown",(t=>{"Escape"===t.key&&this.open&&(t.preventDefault(),t.stopPropagation(),this.open=!1,this.button.focus())})),this.addEventListener("focusout",(t=>{t.relatedTarget instanceof Node&&this.contains(t.relatedTarget)||(this.open=!1)})),this.content.addEventListener("click",(t=>{t.target instanceof Element&&t.target.closest("[role=menuitem], [role=menuitemradio]")&&(this.open=!1,this.button.focus())}))}disconnectedCallback(){s(this.backdrop),s(this.content)}get open(){return this.hasAttribute("open")}set open(t){t?this.setAttribute("open",""):this.removeAttribute("open")}attributeChangedCallback(t,e,i){"open"===t&&(null!==i?this.wasOpened():this.wasClosed())}wasOpened(){if(!this.content.hidden)return;this.content.hidden=!1,this.backdrop.hidden=!1,e(this.content),e(this.backdrop),this.button.setAttribute("aria-expanded","true"),this.content.style.position="absolute",this.cleanup=r(this.button,this.content,(()=>{h(this.button,this.content,{placement:this.getAttribute("placement")||"bottom",middleware:[d(),c(),l({apply:({availableWidth:t,availableHeight:e})=>{Object.assign(this.content.style,{maxWidth:"",maxHeight:""});const i=getComputedStyle(this.content);("none"===i.maxWidth||t<parseInt(i.maxWidth))&&(this.content.style.maxWidth=`${t}px`),("none"===i.maxHeight||e<parseInt(i.maxHeight))&&(this.content.style.maxHeight=`${e}px`)}})]}).then((({x:t,y:e,placement:i})=>{Object.assign(this.content.style,{left:`${t}px`,top:`${e}px`}),this.content.dataset.placement=i}))}),{ancestorScroll:!1});const t=this.content.querySelector("[autofocus]");t?t.focus():this.content.focus(),this.dispatchEvent(new Event("open"))}wasClosed(){this.content.hidden||(this.button.setAttribute("aria-expanded","false"),this.cleanup?.(),i(this.backdrop,{finish:()=>this.backdrop.hidden=!0}),i(this.content,{finish:()=>this.content.hidden=!0}),this.dispatchEvent(new Event("close")))}get backdrop(){return this.shadowRoot?.firstElementChild}get button(){return this.children[0]}get content(){return this.children[1]}}class v extends HTMLElement{connectedCallback(){this.hasAttribute("role")||this.setAttribute("role","toolbar"),this.setAttribute("tabindex","0"),this.addEventListener("focus",this.onInitialFocus,{once:!0}),this.addEventListener("keydown",this.onKeyDown)}disconnectedCallback(){this.removeEventListener("keydown",this.onKeyDown)}onInitialFocus=t=>{this.removeAttribute("tabindex"),this.focusControlAtIndex(0)};onKeyDown=t=>{if("ArrowRight"!==t.key&&"ArrowLeft"!==t.key&&"Home"!==t.key&&"End"!==t.key)return;const e=this.controls,i=this.controls.length,s=e.indexOf(t.target);if(-1===s)return;let n=0;"ArrowLeft"===t.key&&(n=s-1),"ArrowRight"===t.key&&(n=s+1),"End"===t.key&&(n=i-1),n<0&&(n=i-1),n>i-1&&(n=0),this.focusControlAtIndex(n),t.preventDefault()};focusControlAtIndex(t){this.controls.forEach(((e,i)=>{i===t?(e.setAttribute("tabindex","0"),e.focus()):e.setAttribute("tabindex","-1")}))}get controls(){return o(this)}}class f extends HTMLElement{static delay=100;static placement="top";static tooltipClass="tooltip";parent;tooltip;timeout;observer;showing=!1;onMouseEnter=this.afterDelay.bind(this,this.show);onFocus=this.show.bind(this);onMouseLeave=this.afterDelay.bind(this,this.hide);onBlur=this.hide.bind(this);connectedCallback(){this.parent=this.parentNode,this.parent&&(this.parent.addEventListener("mouseenter",this.onMouseEnter),this.parent.addEventListener("focus",this.onFocus),this.parent.addEventListener("mouseleave",this.onMouseLeave),this.parent.addEventListener("blur",this.onBlur),this.parent.addEventListener("click",this.onBlur),this.observer=new MutationObserver((t=>{t.forEach((t=>{"disabled"===t.attributeName&&this.hide()}))})),this.observer.observe(this.parent,{attributes:!0})),document.addEventListener("keydown",this.onKeyDown),document.addEventListener("scroll",this.onBlur)}disconnectedCallback(){this.tooltip&&(this.tooltip.remove(),this.tooltip=void 0),this.parent&&(this.parent.removeEventListener("mouseenter",this.onMouseEnter),this.parent.removeEventListener("focus",this.onFocus),this.parent.removeEventListener("mouseleave",this.onMouseLeave),this.parent.removeEventListener("blur",this.onBlur),this.parent.removeEventListener("click",this.onBlur),this.parent=void 0),document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("scroll",this.onBlur),this.observer.disconnect(),clearTimeout(this.timeout)}get disabled(){return this.hasAttribute("disabled")}set disabled(t){t?this.setAttribute("disabled",""):this.removeAttribute("disabled")}onKeyDown=t=>{"Escape"===t.key&&this.hide()};show(){if(this.disabled)return;const t=this.createTooltip();clearTimeout(this.timeout),this.showing||(t.hidden=!1,e(t),this.showing=!0),t.innerHTML!==this.innerHTML&&(t.innerHTML=this.innerHTML),t.style.position="absolute",h(this.parent,t,{placement:this.getAttribute("placement")||f.placement,middleware:[d(),c()]}).then((({x:e,y:i,placement:s})=>{Object.assign(t.style,{left:`${e}px`,top:`${i}px`}),t.dataset.placement=s}))}hide(){clearTimeout(this.timeout),this.showing&&(this.showing=!1,i(this.tooltip,{finish:()=>{this.tooltip&&(this.tooltip.hidden=!0)}}))}afterDelay(t){clearTimeout(this.timeout);const e=parseInt(this.getAttribute("delay")||"");this.timeout=window.setTimeout(t.bind(this),isNaN(e)?f.delay:e)}createTooltip(){return this.tooltip||(this.tooltip=document.createElement("div"),this.tooltip.className=this.getAttribute("tooltip-class")||f.tooltipClass,this.tooltip.hidden=!0,this.tooltip.addEventListener("mouseenter",this.show.bind(this)),this.tooltip.addEventListener("mouseleave",this.afterDelay.bind(this,this.hide)),document.body.appendChild(this.tooltip)),this.tooltip}}export{u as AlertsElement,p as MenuElement,m as ModalElement,b as PopupElement,v as ToolbarElement,f as TooltipElement};
@@ -1,10 +0,0 @@
1
- export default class MenuElement extends HTMLElement {
2
- static searchDelay: number;
3
- private search;
4
- private searchTimeout?;
5
- connectedCallback(): void;
6
- disconnectedCallback(): void;
7
- private get items();
8
- private onKeyDown;
9
- private navigate;
10
- }
@@ -1,17 +0,0 @@
1
- export default class ModalElement extends HTMLElement {
2
- static attention: (el: Element) => void;
3
- static get observedAttributes(): string[];
4
- private focusTrap?;
5
- private connected;
6
- constructor();
7
- connectedCallback(): void;
8
- disconnectedCallback(): void;
9
- get open(): boolean;
10
- set open(val: boolean);
11
- close(): void;
12
- attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
13
- private wasOpened;
14
- private wasClosed;
15
- private get backdrop();
16
- private get content();
17
- }
@@ -1,15 +0,0 @@
1
- export default class PopupElement extends HTMLElement {
2
- cleanup?: () => void;
3
- static get observedAttributes(): string[];
4
- constructor();
5
- connectedCallback(): void;
6
- disconnectedCallback(): void;
7
- get open(): boolean;
8
- set open(val: boolean);
9
- attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
10
- private wasOpened;
11
- private wasClosed;
12
- private get backdrop();
13
- private get button();
14
- private get content();
15
- }
@@ -1,8 +0,0 @@
1
- export default class ToolbarElement extends HTMLElement {
2
- connectedCallback(): void;
3
- disconnectedCallback(): void;
4
- private onInitialFocus;
5
- private onKeyDown;
6
- private focusControlAtIndex;
7
- private get controls();
8
- }
@@ -1,23 +0,0 @@
1
- export default class TooltipElement extends HTMLElement {
2
- static delay: number;
3
- static placement: string;
4
- static tooltipClass: string;
5
- private parent?;
6
- private tooltip?;
7
- private timeout?;
8
- private observer?;
9
- private showing;
10
- private onMouseEnter;
11
- private onFocus;
12
- private onMouseLeave;
13
- private onBlur;
14
- connectedCallback(): void;
15
- disconnectedCallback(): void;
16
- get disabled(): boolean;
17
- set disabled(val: boolean);
18
- private onKeyDown;
19
- private show;
20
- private hide;
21
- private afterDelay;
22
- private createTooltip;
23
- }