@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
313 lines (306 loc) • 15 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
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-55f8a3b7.js');
const array = require('./array-056ac2a2.js');
const dom = require('./dom-18ca68ff.js');
const form = require('./form-f9d34433.js');
const guid = require('./guid-db20443e.js');
const interactive = require('./interactive-26294f2c.js');
const label = require('./label-fb3080ea.js');
const loadable = require('./loadable-53f729bb.js');
require('./resources-45d84c94.js');
require('./browser-28ea2ce1.js');
const CSS = {
container: "container"
};
const radioButtonCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:block;cursor:pointer}:host .container{position:relative;outline:2px solid transparent;outline-offset:2px}:host .radio{cursor:pointer;border-radius:9999px;background-color:var(--calcite-ui-foreground-1);outline-color:transparent;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;box-shadow:inset 0 0 0 1px var(--calcite-ui-border-input)}:host([hovered]) .radio,:host(:not([checked])[focused]:not([disabled])) .radio{box-shadow:inset 0 0 0 2px var(--calcite-ui-brand)}:host([focused]) .radio{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n 2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}:host([disabled]) .radio{cursor:default;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([hovered][disabled]) .radio{box-shadow:inset 0 0 0 1px var(--calcite-ui-border-input)}:host([scale=s]){--calcite-radio-size:var(--calcite-font-size--2)}:host([scale=m]){--calcite-radio-size:var(--calcite-font-size--1)}:host([scale=l]){--calcite-radio-size:var(--calcite-font-size-0)}.radio{block-size:var(--calcite-radio-size);max-inline-size:var(--calcite-radio-size);min-inline-size:var(--calcite-radio-size)}:host([scale=s][checked]) .radio,:host([hovered][scale=s][checked][disabled]) .radio{box-shadow:inset 0 0 0 4px var(--calcite-ui-brand)}:host([scale=s][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 4px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1)}:host([scale=m][checked]) .radio,:host([hovered][scale=m][checked][disabled]) .radio{box-shadow:inset 0 0 0 5px var(--calcite-ui-brand)}:host([scale=m][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 5px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1)}:host([scale=l][checked]) .radio,:host([hovered][scale=l][checked][disabled]) .radio{box-shadow:inset 0 0 0 6px var(--calcite-ui-brand)}:host([scale=l][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 6px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1)}@media (forced-colors: active){:host([checked]) .radio::after,:host([checked][disabled]) .radio::after{content:\"\";inline-size:var(--calcite-radio-size);block-size:var(--calcite-radio-size);background-color:windowText;display:block}}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
const RadioButton = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.calciteInternalRadioButtonBlur = index.createEvent(this, "calciteInternalRadioButtonBlur", 6);
this.calciteRadioButtonChange = index.createEvent(this, "calciteRadioButtonChange", 6);
this.calciteInternalRadioButtonCheckedChange = index.createEvent(this, "calciteInternalRadioButtonCheckedChange", 6);
this.calciteInternalRadioButtonFocus = index.createEvent(this, "calciteInternalRadioButtonFocus", 6);
this.selectItem = (items, selectedIndex) => {
items[selectedIndex].click();
};
this.queryButtons = () => {
return Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden])")).filter((radioButton) => radioButton.name === this.name);
};
this.isFocusable = () => {
const radioButtons = this.queryButtons();
const firstFocusable = radioButtons.find((radioButton) => !radioButton.disabled);
const checked = radioButtons.find((radioButton) => radioButton.checked);
return firstFocusable === this.el && !checked;
};
this.check = () => {
if (this.disabled) {
return;
}
this.focused = true;
this.setFocus();
if (this.checked) {
return;
}
this.uncheckAllRadioButtonsInGroup();
this.checked = true;
this.calciteRadioButtonChange.emit();
};
this.clickHandler = () => {
if (this.disabled) {
return;
}
this.check();
};
this.setContainerEl = (el) => {
this.containerEl = el;
};
this.handleKeyDown = (event) => {
const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "];
const { key } = event;
const { el } = this;
if (keys.indexOf(key) === -1) {
return;
}
if (key === " ") {
this.check();
event.preventDefault();
return;
}
let adjustedKey = key;
if (dom.getElementDir(el) === "rtl") {
if (key === "ArrowRight") {
adjustedKey = "ArrowLeft";
}
if (key === "ArrowLeft") {
adjustedKey = "ArrowRight";
}
}
const radioButtons = Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden]")).filter((radioButton) => radioButton.name === this.name);
let currentIndex = 0;
const radioButtonsLength = radioButtons.length;
radioButtons.some((item, index) => {
if (item.checked) {
currentIndex = index;
return true;
}
});
switch (adjustedKey) {
case "ArrowLeft":
case "ArrowUp":
event.preventDefault();
this.selectItem(radioButtons, array.getRoundRobinIndex(Math.max(currentIndex - 1, -1), radioButtonsLength));
return;
case "ArrowRight":
case "ArrowDown":
event.preventDefault();
this.selectItem(radioButtons, array.getRoundRobinIndex(currentIndex + 1, radioButtonsLength));
return;
default:
return;
}
};
this.onContainerBlur = () => {
this.focused = false;
this.calciteInternalRadioButtonBlur.emit();
};
this.onContainerFocus = () => {
if (!this.disabled) {
this.focused = true;
this.calciteInternalRadioButtonFocus.emit();
}
};
this.checked = false;
this.disabled = false;
this.focused = false;
this.form = undefined;
this.guid = undefined;
this.hidden = false;
this.hovered = false;
this.label = undefined;
this.name = undefined;
this.required = false;
this.scale = "m";
this.value = undefined;
}
checkedChanged(newChecked) {
if (newChecked) {
this.uncheckOtherRadioButtonsInGroup();
}
this.calciteInternalRadioButtonCheckedChange.emit();
}
disabledChanged() {
this.updateTabIndexOfOtherRadioButtonsInGroup();
}
hiddenChanged() {
this.updateTabIndexOfOtherRadioButtonsInGroup();
}
nameChanged() {
this.checkLastRadioButton();
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await loadable.componentLoaded(this);
if (!this.disabled) {
dom.focusElement(this.containerEl);
}
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
syncHiddenFormInput(input) {
input.type = "radio";
}
onLabelClick(event) {
if (this.disabled || this.hidden) {
return;
}
const label = event.currentTarget;
const radioButton = label.for
? this.rootNode.querySelector(`calcite-radio-button[id="${label.for}"]`)
: label.querySelector(`calcite-radio-button[name="${this.name}"]`);
if (!radioButton) {
return;
}
radioButton.focused = true;
this.setFocus();
if (radioButton.checked) {
return;
}
this.uncheckOtherRadioButtonsInGroup();
radioButton.checked = true;
this.calciteRadioButtonChange.emit();
}
checkLastRadioButton() {
const radioButtons = this.queryButtons();
const checkedRadioButtons = radioButtons.filter((radioButton) => radioButton.checked);
if (checkedRadioButtons?.length > 1) {
const lastCheckedRadioButton = checkedRadioButtons[checkedRadioButtons.length - 1];
checkedRadioButtons
.filter((checkedRadioButton) => checkedRadioButton !== lastCheckedRadioButton)
.forEach((checkedRadioButton) => {
checkedRadioButton.checked = false;
checkedRadioButton.emitCheckedChange();
});
}
}
/** @internal */
async emitCheckedChange() {
this.calciteInternalRadioButtonCheckedChange.emit();
}
uncheckAllRadioButtonsInGroup() {
const radioButtons = this.queryButtons();
radioButtons.forEach((radioButton) => {
if (radioButton.checked) {
radioButton.checked = false;
radioButton.focused = false;
}
});
}
uncheckOtherRadioButtonsInGroup() {
const radioButtons = this.queryButtons();
const otherRadioButtons = radioButtons.filter((radioButton) => radioButton.guid !== this.guid);
otherRadioButtons.forEach((otherRadioButton) => {
if (otherRadioButton.checked) {
otherRadioButton.checked = false;
otherRadioButton.focused = false;
}
});
}
updateTabIndexOfOtherRadioButtonsInGroup() {
const radioButtons = this.queryButtons();
const otherFocusableRadioButtons = radioButtons.filter((radioButton) => radioButton.guid !== this.guid && !radioButton.disabled);
otherFocusableRadioButtons.forEach((radioButton) => {
index.forceUpdate(radioButton);
});
}
getTabIndex() {
if (this.disabled) {
return undefined;
}
return this.checked || this.isFocusable() ? 0 : -1;
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
pointerEnterHandler() {
if (this.disabled) {
return;
}
this.hovered = true;
}
pointerLeaveHandler() {
if (this.disabled) {
return;
}
this.hovered = false;
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
this.rootNode = this.el.getRootNode();
this.guid = this.el.id || `calcite-radio-button-${guid.guid()}`;
if (this.name) {
this.checkLastRadioButton();
}
interactive.connectInteractive(this);
label.connectLabel(this);
form.connectForm(this);
this.updateTabIndexOfOtherRadioButtonsInGroup();
}
componentWillLoad() {
loadable.setUpLoadableComponent(this);
}
componentDidLoad() {
loadable.setComponentLoaded(this);
if (this.focused && !this.disabled) {
this.setFocus();
}
}
disconnectedCallback() {
interactive.disconnectInteractive(this);
label.disconnectLabel(this);
form.disconnectForm(this);
this.updateTabIndexOfOtherRadioButtonsInGroup();
}
componentDidRender() {
interactive.updateHostInteraction(this);
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
const tabIndex = this.getTabIndex();
return (index.h(index.Host, { onClick: this.clickHandler, onKeyDown: this.handleKeyDown }, index.h("div", { "aria-checked": dom.toAriaBoolean(this.checked), "aria-label": label.getLabelText(this), class: CSS.container, onBlur: this.onContainerBlur, onFocus: this.onContainerFocus, role: "radio", tabIndex: tabIndex,
// eslint-disable-next-line react/jsx-sort-props
ref: this.setContainerEl }, index.h("div", { class: "radio" })), index.h(form.HiddenFormInputSlot, { component: this })));
}
get el() { return index.getElement(this); }
static get watchers() { return {
"checked": ["checkedChanged"],
"disabled": ["disabledChanged"],
"hidden": ["hiddenChanged"],
"name": ["nameChanged"]
}; }
};
RadioButton.style = radioButtonCss;
exports.calcite_radio_button = RadioButton;