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,GAAA,iBAAS,EAAE,CAAA,GAAA,yCAAU;IACtC,IAAI,aAAC,SAAS,EAAC,GAAG,CAAA,GAAA,gBAAQ;IAC1B,QAAQ,CAAA,GAAA,yCAAW,EAAE;IACrB,qBACE,gBAAC,CAAA,GAAA,aAAS;QACP,GAAG,KAAK;QACT,KAAK;QACL,UAAU;QACV,OAAO;QACP,WAAW,CAAA,cAAe,mBAAmB,8BAAQ;gBAAC,GAAG,WAAW;0BAAE;gBAAU,MAAM,MAAM,IAAI,IAAI;YAAG,GAAG,MAAM,MAAM;kBACrH,CAAA,4BACC;;kCACE,gBAAC,CAAA,GAAA,yCAAa;kCACZ,cAAA,gBAAC;4BACC,WAAW,4BAAM;gCACf,GAAG,WAAW;gCACd,MAAM,MAAM,IAAI,IAAI;gCACpB,cAAc,MAAM,YAAY;4BAClC;sCACA,cAAA,gBAAC;gCACC,KAAK;gCACL,OAAO,CAAA,GAAA,yCAAS,EAAE,WAAW,sCAAgB;oCAAC,GAAG,WAAW;+CAAE;gCAAS;gCACvE,WAAW,6BAAO;;;;oBAGvB;;;;AAKX","sources":["packages/@react-spectrum/s2/src/Switch.tsx"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {\n Switch as AriaSwitch,\n SwitchProps as AriaSwitchProps,\n ContextValue,\n SwitchRenderProps,\n useLocale\n} from 'react-aria-components';\nimport {baseColor, focusRing, fontRelative, style} from '../style' with {type: 'macro'};\nimport {CenterBaseline} from './CenterBaseline';\nimport {controlFont, controlSize, getAllowedOverrides, StyleProps} from './style-utils' with {type: 'macro'};\nimport {createContext, forwardRef, ReactNode, useContext, useRef} from 'react';\nimport {Direction, FocusableRef, FocusableRefValue, GlobalDOMAttributes} from '@react-types/shared';\nimport {FormContext, useFormProps} from './Form';\nimport {pressScale} from './pressScale';\nimport {useFocusableRef} from '@react-spectrum/utils';\nimport {useSpectrumContextProps} from './useSpectrumContextProps';\n\ninterface SwitchStyleProps {\n /**\n * The size of the Switch.\n *\n * @default 'M'\n */\n size?: 'S' | 'M' | 'L' | 'XL',\n /**\n * Whether the Switch should be displayed with an emphasized style.\n */\n isEmphasized?: boolean\n}\n\ninterface RenderProps extends SwitchRenderProps, SwitchStyleProps {}\n\nexport interface SwitchProps extends Omit<AriaSwitchProps, 'className' | 'style' | 'children' | 'onHover' | 'onHoverStart' | 'onHoverEnd' | 'onHoverChange' | keyof GlobalDOMAttributes>, StyleProps, SwitchStyleProps {\n children?: ReactNode\n}\n\nexport const SwitchContext = createContext<ContextValue<Partial<SwitchProps>, FocusableRefValue<HTMLLabelElement>>>(null);\n\nconst wrapper = style({\n display: 'flex',\n columnGap: 'text-to-control',\n alignItems: 'baseline',\n width: 'fit',\n font: controlFont(),\n transition: 'colors',\n color: {\n default: baseColor('neutral'),\n forcedColors: 'ButtonText',\n isDisabled: {\n default: 'disabled',\n forcedColors: 'GrayText'\n }\n },\n gridColumnStart: {\n isInForm: 'field'\n },\n disableTapHighlight: true\n}, getAllowedOverrides());\n\nconst track = style<RenderProps>({\n ...focusRing(),\n borderRadius: 'full',\n '--trackWidth': {\n type: 'width',\n value: fontRelative(26)\n },\n '--trackHeight': {\n type: 'height',\n value: controlSize('sm')\n },\n width: '--trackWidth',\n height: '--trackHeight',\n boxSizing: 'border-box',\n borderWidth: 2,\n borderStyle: 'solid',\n transition: 'default',\n forcedColorAdjust: 'none',\n borderColor: {\n default: baseColor('gray-800'),\n forcedColors: 'ButtonBorder',\n isDisabled: {\n default: 'gray-400',\n forcedColors: 'GrayText'\n },\n isSelected: 'transparent'\n },\n backgroundColor: {\n default: 'gray-25',\n isSelected: {\n default: baseColor('neutral'),\n isEmphasized: baseColor('accent-900'),\n forcedColors: 'Highlight',\n isDisabled: {\n default: 'gray-400',\n forcedColors: 'GrayText'\n }\n }\n }\n});\n\nconst handle = style<RenderProps>({\n height: 'full',\n aspectRatio: 'square',\n borderRadius: 'full',\n backgroundColor: {\n default: baseColor('neutral'),\n isDisabled: {\n default: 'gray-400',\n forcedColors: 'GrayText'\n },\n isSelected: 'gray-25'\n },\n transition: 'default'\n});\n\n// Use an inline style to calculate the transform so we can combine it with the press scale.\nconst transformStyle = ({isSelected, direction}: SwitchRenderProps & {direction: Direction}) => {\n // In the default state, the handle is 8px smaller than the track. When selected it grows to 6px smaller than the track.\n // Normally this could be calculated as a scale transform with (trackHeight - 8px) / trackHeight, however,\n // CSS does not allow division with units. To solve this we use a 3d perspective transform. Perspective is the\n // distance from the Z=0 plane to the viewer. Since we want to scale the handle by a fixed amount and we cannot divide\n // by a value with units, we can set the Z translation to a fixed amount and change the perspective in order to achieve\n // the desired effect. Given the following formula:\n //\n // scale = perspective / (perspective - translateZ)\n //\n // and desired scale factors (accounting for the 2px border on each side of the track):\n //\n // defaultScale = (trackHeight - 8px) / (trackHeight - 4px)\n // selectedScale = (trackHeight - 6px) / (trackHeight - 4px)\n //\n // we can solve for the perspective needed in each case where translateZ is hard coded to -4px:\n //\n // defaultPerspective = trackHeight - 8px\n // selectedPerspective = 2 * (trackHeight - 6px)\n const placement =\n direction === 'ltr'\n ? // The selected state also translates the X position to the end of the track (minus the borders).\n 'translateX(calc(var(--trackWidth) - 100% - 4px)) perspective(calc(2 * (var(--trackHeight) - 6px))) translateZ(-4px)'\n : 'translateX(calc(100% - var(--trackWidth) + 4px)) perspective(calc(2 * (var(--trackHeight) - 6px))) translateZ(-4px)';\n return {\n transform: isSelected ? placement : 'perspective(calc(var(--trackHeight) - 8px)) translateZ(-4px)'\n };\n};\n\n/**\n * Switches allow users to turn an individual option on or off.\n * They are usually used to activate or deactivate a specific setting.\n */\nexport const Switch = /*#__PURE__*/ forwardRef(function Switch(props: SwitchProps, ref: FocusableRef<HTMLLabelElement>) {\n [props, ref] = useSpectrumContextProps(props, ref, SwitchContext);\n let {children, UNSAFE_className = '', UNSAFE_style} = props;\n let inputRef = useRef<HTMLInputElement | null>(null);\n let domRef = useFocusableRef(ref, inputRef);\n let handleRef = useRef(null);\n let isInForm = !!useContext(FormContext);\n let {direction} = useLocale();\n props = useFormProps(props);\n return (\n <AriaSwitch\n {...props}\n ref={domRef}\n inputRef={inputRef}\n style={UNSAFE_style}\n className={renderProps => UNSAFE_className + wrapper({...renderProps, isInForm, size: props.size || 'M'}, props.styles)}>\n {renderProps => (\n <>\n <CenterBaseline>\n <div\n className={track({\n ...renderProps,\n size: props.size || 'M',\n isEmphasized: props.isEmphasized\n })}>\n <div\n ref={handleRef}\n style={pressScale(handleRef, transformStyle)({...renderProps, direction})}\n className={handle(renderProps)} />\n </div>\n </CenterBaseline>\n {children}\n </>\n )}\n </AriaSwitch>\n );\n});\n"],"names":[],"version":3,"file":"Switch.mjs.map"}