UNPKG

@react-spectrum/s2

Version:
1 lines 7.75 kB
{"mappings":"ACiDgB;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAoBF;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAyCC;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;EAAA;;;;;AA7DC;EAAA;;;;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;;;;;AAAA;EAAA;IAAA;;;;IAAA;;;;IAAA;;;;IAAA;;;;IAAA;;;;;;AAAA;EAAA;IAAA;;;;IAoBF;;;;IAAA;;;;IAAA;;;;IAAA","sources":["dcb0a1e1b828d584","packages/@react-spectrum/s2/src/Switch.tsx"],"sourcesContent":["@import \"b4e99f1579042857\";\n@import \"8f3fcc7ad85a1ee6\";\n@import \"a22e7952bd491c56\";\n","/*\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} 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 {FocusableRef, FocusableRefValue} 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'>, 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 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}: SwitchRenderProps) => ({\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 transform: isSelected\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 : 'perspective(calc(var(--trackHeight) - 8px)) translateZ(-4px)'\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 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)}\n className={handle(renderProps)} />\n </div>\n </CenterBaseline>\n {children}\n </>\n )}\n </AriaSwitch>\n );\n});\n"],"names":[],"version":3,"file":"Switch.css.map"}