UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

5 lines (4 loc) 9.46 kB
/*! All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://github.com/Esri/calcite-design-system/blob/dev/LICENSE.md for details. v3.2.1 */ import{c as z,e as A,h as F}from"./ZAT242GT.js";import{c as I,d as C,e as R}from"./JSDTVKI3.js";import{a as x,b as w}from"./BVJFCLMH.js";import{a as l}from"./IFR7DE3R.js";import{b as v}from"./5RDOSP2E.js";import{e as B,l as g}from"./3ADX47DD.js";import{d as y}from"./HPN2C7M6.js";import"./JOSABGK6.js";import"./NNVH7JUI.js";import{E as m,F as d,R as k,c as b,d as f,h as p,q as a}from"./BJZTU5BQ.js";var L={container:"container",radio:"radio"},q=b`:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-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;outline-color:transparent;transition-property:background-color,block-size,border-color,box-shadow,color,inset-block-end,inset-block-start,inset-inline-end,inset-inline-start,inset-size,opacity,outline-color,transform;transition-duration:var(--calcite-animation-timing);transition-timing-function:ease-in-out;border-radius:var(--calcite-radio-button-corner-radius, var(--calcite-corner-radius-pill));background-color:var(--calcite-radio-button-background-color, var(--calcite-color-foreground-1));box-shadow:inset 0 0 0 var(--calcite-border-width-sm) var(--calcite-radio-button-border-color, var(--calcite-color-border-input))}:host([hovered]) .radio,:host(:not([checked])[focused]:not([disabled])) .radio{box-shadow:inset 0 0 0 var(--calcite-border-width-md) var(--calcite-radio-button-border-color, var(--calcite-color-brand))}:host([focused]) .radio{outline:2px solid var(--calcite-color-focus, var(--calcite-ui-focus-color, var(--calcite-color-brand)));outline-offset:calc(2px*(1 - (2*clamp(0,var(--calcite-offset-invert-focus),1))))}:host([disabled]) .radio{cursor:default;opacity:var(--calcite-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.interaction-container{display:contents}:host([hovered][disabled]) .radio{box-shadow:inset 0 0 0 var(--calcite-border-width-sm) var(--calcite-radio-button-border-color, var(--calcite-color-border-input))}:host([scale=s]){--calcite-internal-radio-size: var( --calcite-radio-button-size, var(--calcite-radio-size, var(--calcite-size-fixed-md)) )}:host([scale=m]){--calcite-internal-radio-size: var( --calcite-radio-button-size, var(--calcite-radio-size, var(--calcite-size-fixed-md-plus)) )}:host([scale=l]){--calcite-internal-radio-size: var( --calcite-radio-button-size, var(--calcite-radio-size, var(--calcite-size-fixed-lg)) )}.radio{block-size:var(--calcite-internal-radio-size);inline-size:var(--calcite-internal-radio-size);size:var(--calcite-internal-radio-size)}:host([scale=s][checked]) .radio,:host([hovered][scale=s][checked][disabled]) .radio{box-shadow:inset 0 0 0 var(--calcite-border-width-lg) var(--calcite-radio-button-border-color, var(--calcite-color-brand))}:host([scale=s][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 var(--calcite-border-width-lg) var(--calcite-radio-button-border-color, var(--calcite-color-brand)),0 0 0 2px var(--calcite-radio-button-background-color, var(--calcite-color-foreground-1))}:host([scale=m][checked]) .radio,:host([hovered][scale=m][checked][disabled]) .radio{box-shadow:inset 0 0 0 5px var(--calcite-radio-button-border-color, var(--calcite-color-brand))}:host([scale=m][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 5px var(--calcite-radio-button-border-color, var(--calcite-color-brand)),0 0 0 2px var(--calcite-radio-button-background-color, var(--calcite-color-foreground-1))}:host([scale=l][checked]) .radio,:host([hovered][scale=l][checked][disabled]) .radio{box-shadow:inset 0 0 0 6px var(--calcite-radio-button-border-color, var(--calcite-color-brand))}:host([scale=l][focused][checked]:not([disabled])) .radio{box-shadow:inset 0 0 0 6px var(--calcite-radio-button-border-color, var(--calcite-color-brand)),0 0 0 2px var(--calcite-radio-button-background-color, var(--calcite-color-foreground-1))}@media (forced-colors: active){:host([checked]) .radio:after,:host([checked][disabled]) .radio:after{content:"";inline-size:var(--calcite-internal-radio-size);block-size:var(--calcite-internal-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}:host([hidden]){display:none}[hidden]{display:none}`,h=class extends m{constructor(){super(),this.checked=!1,this.disabled=!1,this.focused=!1,this.hovered=!1,this.required=!1,this.scale="m",this.calciteInternalRadioButtonBlur=a({cancelable:!1}),this.calciteInternalRadioButtonCheckedChange=a({cancelable:!1}),this.calciteInternalRadioButtonFocus=a({cancelable:!1}),this.calciteRadioButtonChange=a({cancelable:!1}),this.listen("pointerenter",this.pointerEnterHandler),this.listen("pointerleave",this.pointerLeaveHandler),this.listen("click",this.clickHandler),this.listen("keydown",this.handleKeyDown)}static{this.properties={checked:[7,{},{reflect:!0,type:Boolean}],disabled:[7,{},{reflect:!0,type:Boolean}],focused:[7,{},{reflect:!0,type:Boolean}],form:[3,{},{reflect:!0}],hovered:[7,{},{reflect:!0,type:Boolean}],label:1,name:[3,{},{reflect:!0}],required:[7,{},{reflect:!0,type:Boolean}],scale:[3,{},{reflect:!0}],value:1}}static{this.styles=q}async emitCheckedChange(){this.calciteInternalRadioButtonCheckedChange.emit()}async setFocus(){await y(this),this.disabled||g(this.containerEl)}connectedCallback(){this.rootNode=this.el.getRootNode(),this.name&&this.checkLastRadioButton(),I(this),z(this),this.updateTabIndexOfOtherRadioButtonsInGroup(),super.connectedCallback()}willUpdate(e){this.hasUpdated&&e.has("checked")&&this.checkedChanged(this.checked),e.has("disabled")&&(this.hasUpdated||this.disabled!==!1)&&this.updateTabIndexOfOtherRadioButtonsInGroup(),e.has("name")&&this.checkLastRadioButton()}updated(){x(this)}loaded(){this.focused&&!this.disabled&&this.setFocus()}disconnectedCallback(){super.disconnectedCallback(),C(this),A(this),this.updateTabIndexOfOtherRadioButtonsInGroup()}checkedChanged(e){e&&this.uncheckOtherRadioButtonsInGroup(),this.calciteInternalRadioButtonCheckedChange.emit()}syncHiddenFormInput(e){e.type="radio"}selectItem(e,o){e[o].click()}queryButtons(){return Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden])")).filter(e=>e.name===this.name)}isFocusable(){let e=this.queryButtons(),o=e.find(i=>!i.disabled),t=e.find(i=>i.checked);return o===this.el&&!t}check(){this.disabled||(this.focused=!0,this.setFocus(),!this.checked&&(this.uncheckAllRadioButtonsInGroup(),this.checked=!0,this.calciteRadioButtonChange.emit()))}clickHandler(){this.disabled||this.check()}onLabelClick(e){if(this.disabled||this.el.hidden)return;let o=e.currentTarget,t=o.for?this.rootNode.querySelector(`calcite-radio-button[id="${o.for}"]`):o.querySelector(`calcite-radio-button[name="${this.name}"]`);t&&(t.focused=!0,this.setFocus(),!t.checked&&(this.uncheckOtherRadioButtonsInGroup(),t.checked=!0,this.calciteRadioButtonChange.emit()))}checkLastRadioButton(){let o=this.queryButtons().filter(t=>t.checked);if(o?.length>1){let t=o[o.length-1];o.filter(i=>i!==t).forEach(i=>{i.checked=!1,i.emitCheckedChange()})}}setContainerEl(e){this.containerEl=e}uncheckAllRadioButtonsInGroup(){this.queryButtons().forEach(o=>{o.checked&&(o.checked=!1,o.focused=!1)})}uncheckOtherRadioButtonsInGroup(){this.queryButtons().filter(t=>t!==this.el).forEach(t=>{t.checked&&(t.checked=!1,t.focused=!1)})}updateTabIndexOfOtherRadioButtonsInGroup(){this.queryButtons().filter(t=>t!==this.el&&!t.disabled).forEach(t=>{t.manager?.component.requestUpdate()})}getTabIndex(){if(!this.disabled)return this.checked||this.isFocusable()?0:-1}pointerEnterHandler(){this.disabled||(this.hovered=!0)}pointerLeaveHandler(){this.disabled||(this.hovered=!1)}handleKeyDown(e){let o=["ArrowLeft","ArrowUp","ArrowRight","ArrowDown"," "],{key:t}=e,{el:i}=this;if(o.indexOf(t)===-1)return;if(t===" "){this.check(),e.preventDefault();return}let c=t;B(i)==="rtl"&&(t==="ArrowRight"&&(c="ArrowLeft"),t==="ArrowLeft"&&(c="ArrowRight"));let r=Array.from(this.rootNode.querySelectorAll("calcite-radio-button:not([hidden])")).filter(n=>n.name===this.name),s=0,u=r.length;switch(r.some((n,E)=>{if(n.checked)return s=E,!0}),c){case"ArrowLeft":case"ArrowUp":e.preventDefault(),this.selectItem(r,l(Math.max(s-1,-1),u));return;case"ArrowRight":case"ArrowDown":e.preventDefault(),this.selectItem(r,l(s+1,u));return;default:return}}onContainerBlur(){this.focused=!1,this.calciteInternalRadioButtonBlur.emit()}onContainerFocus(){this.disabled||(this.focused=!0,this.calciteInternalRadioButtonFocus.emit())}render(){let e=this.getTabIndex();return w({disabled:this.disabled,children:f`<div .ariaChecked=${this.checked} .ariaLabel=${R(this)} class=${d(L.container)} @blur=${this.onContainerBlur} @focus=${this.onContainerFocus} role=radio tabindex=${e??p} ${v(this.setContainerEl)}><div class=${d(L.radio)}></div></div>${F({component:this})}`})}};k("calcite-radio-button",h);export{h as RadioButton};