UNPKG

@atlaskit/toggle

Version:

A toggle is used to view or switch between enabled or disabled states.

137 lines (136 loc) 5.33 kB
/* toggle.tsx generated by @compiled/babel-plugin v0.38.1 */ import _extends from "@babel/runtime/helpers/extends"; import "./toggle.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; import React, { forwardRef, memo, useState } from 'react'; import { usePlatformLeafEventHandler } from '@atlaskit/analytics-next'; import __noop from '@atlaskit/ds-lib/noop'; import { useId } from '@atlaskit/ds-lib/use-id'; import CheckMarkIcon from '@atlaskit/icon/core/check-mark'; import CloseIcon from '@atlaskit/icon/core/cross'; import Spinner from '@atlaskit/spinner'; import { B200, G400, G500, N0, N20, N200, N400, N70 } from '@atlaskit/theme/colors'; import VisuallyHidden from '@atlaskit/visually-hidden'; import IconContainer from './icon-container'; const LOADING_LABEL = ', Loading'; const basicStyles = null; const sizeStyles = { regular: "_2rko1rr0 _4t3ipxbi _1bsbxy5q _kfgtutpp _1mp41crf _p9c41fbe", large: "_2rko1rr0 _4t3iv47k _1bsb1jfw _kfgtpxbi _1mp47vkz _p9c4w12q" }; const noop = __noop; const analyticsAttributes = { componentName: 'toggle', packageName: "@atlaskit/toggle", packageVersion: "0.0.0-development" }; /** * __Toggle__ * * A toggle is used to view or switch between enabled or disabled states. * * - [Examples](https://atlassian.design/components/toggle/examples) * - [Code](https://atlassian.design/components/toggle/code) * - [Usage](https://atlassian.design/components/toggle/usage) */ const Toggle = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, ref) => { const { defaultChecked = false, isDisabled = false, onBlur: providedOnBlur = noop, onChange: providedChange = noop, onFocus: providedFocus = noop, size = 'regular', name = '', value = '', isChecked, isLoading, analyticsContext, id, testId, label, descriptionId } = props; const isControlled = typeof isChecked === 'undefined'; const [checked, setChecked] = useState(defaultChecked); const loadingLabelId = useId(); const handleBlur = usePlatformLeafEventHandler({ fn: providedOnBlur, action: 'blur', analyticsData: analyticsContext, ...analyticsAttributes }); const handleFocus = usePlatformLeafEventHandler({ fn: providedFocus, action: 'focus', analyticsData: analyticsContext, ...analyticsAttributes }); const handleChange = usePlatformLeafEventHandler({ fn: (event, analyticsEvent) => { if (isControlled) { setChecked(checked => !checked); } return providedChange(event, analyticsEvent); }, action: 'change', analyticsData: analyticsContext, ...analyticsAttributes }); const shouldChecked = isControlled ? checked : isChecked; const controlProps = { 'data-checked': shouldChecked ? shouldChecked : undefined, 'data-disabled': isDisabled ? isDisabled : undefined, 'data-size': size, 'data-testid': testId ? testId : undefined }; const labelId = useId(); return /*#__PURE__*/React.createElement("label", _extends({}, controlProps, { className: ax(["_v564kete _1h6d1j28 _1dqonqa1 _189e1dm9 _1e0c1o8l _vchh1ntv _kqswh2mm _1y1m1ntv _bfhk76s1 _syaz5w2r _6rthv77o _1pfhv77o _12l2v77o _ahbqv77o _85i5v77o _1q51v77o _y4tiv77o _bozgv77o _1i2uidpf _1i3tidpf _se28idpf _3tunidpf _1423idpf _1hrtidpf _1bz2idpf _rqfvidpf _1yc0glyw _onsr1snc _1vgmagmp _1av610yn _1ulhidpf _rfx31qll _pdykkete _1cs8stnw _1rus5w2r _1kt9b3bt _1fkr1y44 _z5wt1y44 _7dwzglyw _1ys4194a _krlfnqa1 _ju3ddkaa _1bki1y1w _1flt1y1w _3xv21y1w _9471g4a2 _ucab5w2r _mqf815t7 _3dj215t7 _1dl915t7 _1div5w2r _1il8kb7n _y54d5w2r _irr353wy _d0altlke _19ja13gf _8muf13gf _1k1q13gf _1plud4ly _1uo853wy _178c1y1w _16rslghj _i5ec1w7i", sizeStyles[size]]) }), label ? /*#__PURE__*/React.createElement("span", { id: labelId, hidden: true }, isLoading ? `${label}${LOADING_LABEL}` : label) : null, /*#__PURE__*/React.createElement("input", { ref: ref, checked: shouldChecked, disabled: isDisabled, id: id, name: name, onBlur: handleBlur, onChange: handleChange, onFocus: handleFocus, type: "checkbox", value: value, "data-testid": testId && `${testId}--input`, "aria-labelledby": isLoading && label ? `${labelId} ${loadingLabelId}` : label ? labelId : undefined, "aria-describedby": descriptionId }), /*#__PURE__*/React.createElement(IconContainer, { size: size, isHidden: !shouldChecked, position: "left" }, isLoading && shouldChecked ? /*#__PURE__*/React.createElement(Spinner, { size: "xsmall", appearance: "invert" }) : /*#__PURE__*/React.createElement(CheckMarkIcon, { label: "", color: "currentColor", testId: testId && `${testId}--toggle-check-icon`, size: "small" })), /*#__PURE__*/React.createElement(IconContainer, { size: size, isHidden: shouldChecked, position: "right" }, isLoading && !shouldChecked ? /*#__PURE__*/React.createElement(Spinner, { size: "xsmall", appearance: "invert" }) : /*#__PURE__*/React.createElement(CloseIcon, { label: "", color: "currentColor", testId: testId && `${testId}--toggle-cross-icon`, size: "small" })), isLoading && !label && /*#__PURE__*/React.createElement(VisuallyHidden, { id: loadingLabelId }, LOADING_LABEL)); })); Toggle.displayName = 'Toggle'; export default Toggle;