@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
153 lines (152 loc) • 4.5 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { Component, Element, Event, Method, Prop, h } from "@stencil/core";
import { CSS, ICONS } from "./resources";
export class Handle {
constructor() {
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* @internal - stores the activated state of the drag handle.
*/
this.activated = false;
/**
* Value for the button title attribute
*/
this.textTitle = "handle";
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
this.handleKeyDown = (event) => {
switch (event.key) {
case " ":
this.activated = !this.activated;
break;
case "ArrowUp":
case "ArrowDown":
if (!this.activated) {
return;
}
const direction = event.key.toLowerCase().replace("arrow", "");
this.calciteHandleNudge.emit({ handle: this.el, direction });
break;
}
};
this.handleBlur = () => {
this.activated = false;
};
}
// --------------------------------------------------------------------------
//
// Methods
//
// --------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
this.handleButton.focus();
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
return (
// Needs to be a span because of https://github.com/SortableJS/Sortable/issues/1486
h("span", { "aria-pressed": this.activated.toString(), class: { [CSS.handle]: true, [CSS.handleActivated]: this.activated }, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, ref: (el) => {
this.handleButton = el;
}, role: "button", tabindex: "0", title: this.textTitle },
h("calcite-icon", { icon: ICONS.drag, scale: "s" })));
}
static get is() { return "calcite-handle"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() { return {
"$": ["handle.scss"]
}; }
static get styleUrls() { return {
"$": ["handle.css"]
}; }
static get properties() { return {
"activated": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": "- stores the activated state of the drag handle."
}],
"text": ""
},
"attribute": "activated",
"reflect": true,
"defaultValue": "false"
},
"textTitle": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Value for the button title attribute"
},
"attribute": "text-title",
"reflect": true,
"defaultValue": "\"handle\""
}
}; }
static get events() { return [{
"method": "calciteHandleNudge",
"name": "calciteHandleNudge",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Emitted when the the handle is activated and the up or down arrow key is pressed."
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}]; }
static get methods() { return {
"setFocus": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Sets focus on the component.",
"tags": []
}
}
}; }
static get elementRef() { return "el"; }
}