@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
307 lines (301 loc) • 14.9 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, proxyCustomElement, HTMLElement, createEvent, Host } from '@stencil/core/internal/client';
import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
import { g as guid } from './guid.js';
import { c as connectInteractive, d as disconnectInteractive, u as updateHostInteraction } from './interactive.js';
import { c as connectLabel, d as disconnectLabel } from './label2.js';
import { a as setUpLoadableComponent, s as setComponentLoaded, c as componentLoaded } from './loadable.js';
import { c as connectLocalized, d as disconnectLocalized } from './locale.js';
import { u as updateMessages, c as connectMessages, s as setUpMessages, d as disconnectMessages } from './t9n.js';
import { d as defineCustomElement$3 } from './chip.js';
import { d as defineCustomElement$2 } from './icon.js';
const StarIcon = ({ full, scale, partial }) => (h("calcite-icon", { ...{
class: partial ? undefined : "icon",
icon: full ? "star-f" : "star",
scale
} }));
const ratingCss = "@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([read-only]) *,:host([disabled]) *,:host([read-only]) ::slotted(*),:host([disabled]) ::slotted(*){pointer-events:none}:host{position:relative;display:flex;align-items:center;inline-size:-moz-fit-content;inline-size:fit-content}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){block-size:1.5rem;--calcite-rating-spacing-unit:0.25rem}:host([scale=m]){block-size:2rem;--calcite-rating-spacing-unit:0.5rem}:host([scale=l]){block-size:2.75rem;--calcite-rating-spacing-unit:0.75rem}.fieldset{margin:0;display:flex;border-width:0;padding:0;align-items:center;gap:var(--calcite-rating-spacing-unit)}.wrapper{display:inline-block}.star{transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;position:relative;display:flex;flex-direction:column;cursor:pointer;color:var(--calcite-ui-border-input)}.focused{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 )}.average,.fraction{color:var(--calcite-ui-warning)}.hovered,.selected{color:var(--calcite-ui-brand)}.fraction{transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;position:absolute;pointer-events:none;inset-block-start:0;overflow:hidden;inset-inline-start:0}calcite-chip{pointer-events:none;cursor:default}.number--average{font-weight:bold}.number--count{color:var(--calcite-ui-text-2);font-style:italic}.number--count:not(:first-child){margin-inline-start:var(--calcite-rating-spacing-unit)}.visually-hidden{position:absolute;inline-size:1px;block-size:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}::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 Rating = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteRatingChange = createEvent(this, "calciteRatingChange", 6);
this.handleRatingPointerOver = () => {
this.isKeyboardInteraction = false;
};
this.handleRatingPointerOut = () => {
this.isKeyboardInteraction = true;
this.hoverValue = null;
this.focusValue = null;
this.hasFocus = false;
};
this.handleRatingFocusIn = () => {
const selectedInput = this.value > 0 ? this.value - 1 : 0;
const focusInput = this.inputRefs[selectedInput];
const focusValue = Number(focusInput.value);
focusInput.select();
this.focusValue = focusValue;
this.hoverValue = focusValue;
this.hasFocus = true;
};
this.handleRatingFocusLeave = () => {
this.focusValue = null;
this.hoverValue = null;
this.hasFocus = false;
};
this.handleHostKeyDown = () => {
this.isKeyboardInteraction = true;
};
this.handleInputKeyDown = (event) => {
const target = event.currentTarget;
const inputVal = Number(target.value);
const key = event.key;
const numberKey = key == " " ? undefined : Number(key);
this.emit = true;
if (isNaN(numberKey)) {
switch (key) {
case "Enter":
case " ":
this.value = !this.required && this.value === inputVal ? 0 : inputVal;
break;
case "ArrowLeft":
this.value = inputVal - 1;
break;
case "ArrowRight":
this.value = inputVal + 1;
break;
case "Tab":
if (this.hasFocus) {
this.hasFocus = false;
this.focusValue = null;
this.hoverValue = null;
}
}
}
else {
if (!this.required && numberKey >= 0 && numberKey <= this.max) {
this.value = numberKey;
}
else if (this.required && numberKey > 0 && numberKey <= this.max) {
this.value = numberKey;
}
}
};
this.handleInputChange = (event) => {
if (this.isKeyboardInteraction === true) {
const inputVal = Number(event.target["value"]);
this.focusValue = inputVal;
this.hoverValue = inputVal;
this.value = inputVal;
}
};
this.handleLabelPointerOver = (event) => {
const target = event.currentTarget;
const newPointerValue = Number(target.firstChild["value"] || 0);
this.hoverValue = newPointerValue;
this.focusValue = null;
};
this.handleLabelPointerDown = (event) => {
const target = event.currentTarget;
const inputVal = Number(target.firstChild["value"] || 0);
this.focusValue = null;
this.hoverValue = null;
this.emit = true;
this.value = !this.required && this.value === inputVal ? 0 : inputVal;
};
this.emit = false;
this.guid = `calcite-ratings-${guid()}`;
this.isKeyboardInteraction = true;
this.max = 5;
this.average = undefined;
this.count = undefined;
this.disabled = false;
this.form = undefined;
this.messages = undefined;
this.messageOverrides = undefined;
this.name = undefined;
this.readOnly = false;
this.required = false;
this.scale = "m";
this.showChip = false;
this.value = 0;
this.effectiveLocale = "";
this.defaultMessages = undefined;
this.hoverValue = undefined;
this.focusValue = undefined;
this.hasFocus = undefined;
}
onMessagesChange() {
/* wired up by t9n util */
}
handleValueUpdate(newValue) {
this.hoverValue = newValue;
this.focusValue = newValue;
if (this.emit) {
this.calciteRatingChange.emit();
}
this.emit = false;
}
effectiveLocaleChange() {
updateMessages(this, this.effectiveLocale);
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectInteractive(this);
connectLocalized(this);
connectMessages(this);
connectLabel(this);
connectForm(this);
}
async componentWillLoad() {
await setUpMessages(this);
setUpLoadableComponent(this);
this.inputRefs = Array(this.max);
}
componentWillRender() {
this.starsMap = Array.from({ length: this.max }, (_, i) => {
const value = i + 1;
const average = !this.focusValue &&
!this.hoverValue &&
this.average &&
!this.value &&
value <= this.average;
const checked = value === this.value;
const focused = this.isKeyboardInteraction && this.hasFocus && this.focusValue === value;
const fraction = this.average && this.average + 1 - value;
const hovered = value <= this.hoverValue;
const id = `${this.guid}-${value}`;
const partial = !this.focusValue &&
!this.hoverValue &&
!this.value &&
!hovered &&
fraction > 0 &&
fraction < 1;
const selected = this.value >= value;
return {
average,
checked,
focused,
fraction,
hovered,
id,
idx: i,
partial,
selected,
value
};
});
}
componentDidLoad() {
setComponentLoaded(this);
}
disconnectedCallback() {
disconnectInteractive(this);
disconnectLocalized(this);
disconnectMessages(this);
disconnectLabel(this);
disconnectForm(this);
}
componentDidRender() {
updateHostInteraction(this);
}
render() {
return (h(Host, { onBlur: this.handleRatingFocusLeave, onFocus: this.handleRatingFocusIn, onKeyDown: this.handleHostKeyDown, onPointerOut: this.handleRatingPointerOut, onPointerOver: this.handleRatingPointerOver }, h("span", { class: "wrapper" }, h("fieldset", { class: "fieldset", disabled: this.disabled }, h("legend", { class: "visually-hidden" }, this.messages.rating), this.starsMap.map(({ average, checked, focused, fraction, hovered, id, idx, partial, selected, value }) => {
return (h("label", { class: {
star: true,
focused,
selected,
hovered,
average,
partial
}, htmlFor: id, onPointerDown: this.handleLabelPointerDown, onPointerOver: this.handleLabelPointerOver }, h("input", { checked: checked, class: "visually-hidden", disabled: this.disabled || this.readOnly, id: id, name: this.guid, onChange: this.handleInputChange, onKeyDown: this.handleInputKeyDown, type: "radio", value: value,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => {
this.inputRefs[idx] = el;
return ((value === 1 || value === this.value) &&
(this.inputFocusRef = el));
} }), h(StarIcon, { full: selected || average, scale: this.scale }), partial && (h("div", { class: "fraction", style: { width: `${fraction * 100}%` } }, h(StarIcon, { full: true, partial: true, scale: this.scale }))), h("span", { class: "visually-hidden" }, this.messages.stars.replace("${num}", `${value}`))));
}), (this.count || this.average) && this.showChip ? (h("calcite-chip", { scale: this.scale, value: this.count?.toString() }, !!this.average && h("span", { class: "number--average" }, this.average.toString()), !!this.count && h("span", { class: "number--count" }, "(", this.count?.toString(), ")"))) : null), h(HiddenFormInputSlot, { component: this }))));
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
onLabelClick() {
this.setFocus();
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
this.inputFocusRef?.focus();
}
static get assetsDirs() { return ["assets"]; }
get el() { return this; }
static get watchers() { return {
"messageOverrides": ["onMessagesChange"],
"value": ["handleValueUpdate"],
"effectiveLocale": ["effectiveLocaleChange"]
}; }
static get style() { return ratingCss; }
}, [1, "calcite-rating", {
"average": [514],
"count": [514],
"disabled": [516],
"form": [513],
"messages": [1040],
"messageOverrides": [1040],
"name": [513],
"readOnly": [516, "read-only"],
"required": [516],
"scale": [513],
"showChip": [516, "show-chip"],
"value": [1538],
"effectiveLocale": [32],
"defaultMessages": [32],
"hoverValue": [32],
"focusValue": [32],
"hasFocus": [32],
"setFocus": [64]
}]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-rating", "calcite-chip", "calcite-icon"];
components.forEach(tagName => { switch (tagName) {
case "calcite-rating":
if (!customElements.get(tagName)) {
customElements.define(tagName, Rating);
}
break;
case "calcite-chip":
if (!customElements.get(tagName)) {
defineCustomElement$3();
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
} });
}
defineCustomElement$1();
const CalciteRating = Rating;
const defineCustomElement = defineCustomElement$1;
export { CalciteRating, defineCustomElement };