@spectrum-web-components/overlay
Version:
An `<sp-overlay>` element is used to decorate content that you would like to present to your visitors as "overlaid" on the rest of the application. This includes dialogs (modal and not), pickers, tooltips, context menus, et al.
3 lines (2 loc) • 3.26 kB
JavaScript
"use strict";import{isAndroid as a,isIOS as d}from"@spectrum-web-components/shared/src/platform.js";import{conditionAttributeWithId as p}from"@spectrum-web-components/base/src/condition-attribute-with-id.js";import{randomID as l}from"@spectrum-web-components/shared/src/random-id.js";import{noop as r}from"./AbstractOverlay.js";import{InteractionController as h,InteractionTypes as c}from"./InteractionController.js";const g=300;export const LONGPRESS_INSTRUCTIONS={touch:"Double tap and long press for additional options",keyboard:"Press Space or Alt+Down Arrow for additional options",mouse:"Click and hold for additional options"};export class LongpressController extends h{constructor(){super(...arguments);this.type=c.longpress;this.longpressState=null;this.releaseDescription=r;this.handlePointerup=()=>{var e;clearTimeout(this.timeout),this.target&&(this.longpressState=((e=this.overlay)==null?void 0:e.state)==="opening"?"pressed":null,document.removeEventListener("pointerup",this.handlePointerup),document.removeEventListener("pointercancel",this.handlePointerup))}}get activelyOpening(){return this.longpressState==="opening"||this.longpressState==="pressed"}handleLongpress(){this.open=!0,this.longpressState=this.longpressState==="potential"?"opening":"pressed"}handlePointerdown(e){!this.target||e.button!==0||(this.longpressState="potential",document.addEventListener("pointerup",this.handlePointerup),document.addEventListener("pointercancel",this.handlePointerup),"holdAffordance"in this.target)||(this.timeout=setTimeout(()=>{this.target&&this.target.dispatchEvent(new CustomEvent("longpress",{bubbles:!0,composed:!0,detail:{source:"pointer"}}))},g))}handleKeydown(e){const{code:t,altKey:o}=e;o&&t==="ArrowDown"&&(e.stopPropagation(),e.stopImmediatePropagation())}handleKeyup(e){const{code:t,altKey:o}=e;if(t==="Space"||o&&t==="ArrowDown"){if(!this.target)return;e.stopPropagation(),this.target.dispatchEvent(new CustomEvent("longpress",{bubbles:!0,composed:!0,detail:{source:"keyboard"}})),setTimeout(()=>{this.longpressState=null})}}prepareDescription(e){if(this.releaseDescription!==r||!this.overlay.elements.length)return;const t=document.createElement("div");t.id=`longpress-describedby-descriptor-${l()}`;const o=d()||a()?"touch":"keyboard";t.textContent=LONGPRESS_INSTRUCTIONS[o],t.slot="longpress-describedby-descriptor";const n=e.getRootNode(),s=this.overlay.getRootNode();n===s?this.overlay.append(t):(t.hidden=!("host"in n),e.insertAdjacentElement("afterend",t));const i=p(e,"aria-describedby",[t.id]);this.releaseDescription=()=>{i(),t.remove(),this.releaseDescription=r}}shouldCompleteOpen(){this.longpressState=this.longpressState==="pressed"?null:this.longpressState}init(){var t;(t=this.abortController)==null||t.abort(),this.abortController=new AbortController;const{signal:e}=this.abortController;this.target.addEventListener("longpress",()=>this.handleLongpress(),{signal:e}),this.target.addEventListener("pointerdown",o=>this.handlePointerdown(o),{signal:e}),this.prepareDescription(this.target),!this.target.holdAffordance&&(this.target.addEventListener("keydown",o=>this.handleKeydown(o),{signal:e}),this.target.addEventListener("keyup",o=>this.handleKeyup(o),{signal:e}))}}
//# sourceMappingURL=LongpressController.js.map