@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
262 lines (261 loc) • 7.43 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.
* v1.5.0-next.4
*/
import { h } from "@stencil/core";
import { toAriaBoolean } from "../../utils/dom";
import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable";
import { connectLocalized, disconnectLocalized } from "../../utils/locale";
import { connectMessages, disconnectMessages, setUpMessages, updateMessages } from "../../utils/t9n";
import { CSS, ICONS } from "./resources";
export class Handle {
constructor() {
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
this.handleKeyDown = (event) => {
switch (event.key) {
case " ":
this.activated = !this.activated;
event.preventDefault();
break;
case "ArrowUp":
if (!this.activated) {
return;
}
event.preventDefault();
this.calciteHandleNudge.emit({ direction: "up" });
break;
case "ArrowDown":
if (!this.activated) {
return;
}
event.preventDefault();
this.calciteHandleNudge.emit({ direction: "down" });
break;
}
};
this.handleBlur = () => {
this.activated = false;
};
this.activated = false;
this.dragHandle = undefined;
this.messages = undefined;
this.messageOverrides = undefined;
this.effectiveLocale = undefined;
this.defaultMessages = undefined;
}
onMessagesChange() {
/* wired up by t9n util */
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectMessages(this);
connectLocalized(this);
}
async componentWillLoad() {
setUpLoadableComponent(this);
await setUpMessages(this);
}
componentDidLoad() {
setComponentLoaded(this);
}
disconnectedCallback() {
disconnectMessages(this);
disconnectLocalized(this);
}
effectiveLocaleChange() {
updateMessages(this, this.effectiveLocale);
}
// --------------------------------------------------------------------------
//
// Methods
//
// --------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
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": toAriaBoolean(this.activated), class: { [CSS.handle]: true, [CSS.handleActivated]: this.activated }, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, role: "button", tabindex: "0", title: this.messages.dragHandle,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => {
this.handleButton = el;
} }, 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 assetsDirs() { return ["assets"]; }
static get properties() {
return {
"activated": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"attribute": "activated",
"reflect": true,
"defaultValue": "false"
},
"dragHandle": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Value for the button title attribute"
},
"attribute": "drag-handle",
"reflect": true
},
"messages": {
"type": "unknown",
"mutable": false,
"complexType": {
"original": "HandleMessages",
"resolved": "{ dragHandle: string; }",
"references": {
"HandleMessages": {
"location": "import",
"path": "./assets/handle/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": "Made into a prop for testing purposes only"
}
},
"messageOverrides": {
"type": "unknown",
"mutable": false,
"complexType": {
"original": "Partial<HandleMessages>",
"resolved": "{ dragHandle?: string; }",
"references": {
"Partial": {
"location": "global"
},
"HandleMessages": {
"location": "import",
"path": "./assets/handle/t9n"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Use this property to override individual strings used by the component."
}
}
};
}
static get states() {
return {
"effectiveLocale": {},
"defaultMessages": {}
};
}
static get events() {
return [{
"method": "calciteHandleNudge",
"name": "calciteHandleNudge",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Emitted when the handle is activated and the up or down arrow key is pressed."
},
"complexType": {
"original": "HandleNudge",
"resolved": "HandleNudge",
"references": {
"HandleNudge": {
"location": "import",
"path": "./interfaces"
}
}
}
}];
}
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"; }
static get watchers() {
return [{
"propName": "messageOverrides",
"methodName": "onMessagesChange"
}, {
"propName": "effectiveLocale",
"methodName": "effectiveLocaleChange"
}];
}
}