UNPKG

@navinc/base-react-components

Version:
131 lines (127 loc) 5.67 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useRef, useEffect } from 'react'; import styled from 'styled-components'; import Copy from './copy.js'; import { focusWithoutScroll } from '@navinc/utils'; import isRebrand from './is-rebrand.js'; const getLabelStyling = ({ isInvalid, theme }) => ` margin: 0; padding: 0; color: ${isInvalid ? theme.error : 'inherit'}; `; const getFillColor = (theme, disabled, isInvalid) => { if (isRebrand(theme)) return isInvalid ? theme.navStatusNegative100 : theme.navSecondary; return disabled ? theme.neutral100 : theme.bubbleBlue100; }; const getBorderColor = (theme, disabled, isInvalid, checked) => { if (isRebrand(theme)) { if (isInvalid) return theme.navStatusNegative; return checked ? theme.navPrimary400 : theme.navNeutral300; } return disabled ? theme.scuttleGray300 : theme.bubbleBlue200; }; const getIndicatorColor = (theme, checked, isInvalid) => { if (isRebrand(theme)) { if (checked) return isInvalid ? theme.navStatusNegative : theme.navPrimary400; return 'transparent'; } return checked ? theme.navPrimary : 'transparent'; }; const CopyLabel = styled(Copy).withConfig({ displayName: "brc-sc-CopyLabel", componentId: "brc-sc-j3svw0" }) ` ${getLabelStyling} `; const DivLabel = styled.div.withConfig({ displayName: "brc-sc-DivLabel", componentId: "brc-sc-eo1nzp" }) ` ${getLabelStyling} `; const RadioItem = styled.label.withConfig({ displayName: "brc-sc-RadioItem", componentId: "brc-sc-dcv7yp" }) ` display: flex; flex-flow: row nowrap; align-items: center; flex: 1 1 auto; margin-bottom: 0; cursor: pointer; text-align: left; ${({ asideContent, hasBorder, theme }) => (asideContent || hasBorder) && ` border-radius: ${isRebrand(theme) ? '8px' : '12px'}; border: 1px solid ${isRebrand(theme) ? theme.navNeutral300 : theme.border}; padding: ${isRebrand(theme) ? '14px 16px' : '8px 16px'}; min-height:${isRebrand(theme) ? '24px' : '62px'}; `} ${({ checked, checkedBackgroundColor }) => checked && checkedBackgroundColor && ` background-color: ${checkedBackgroundColor}; border-color: ${checkedBackgroundColor}; `} & > :first-child { margin-right: 16px; } `; const AsideContainer = styled.div.withConfig({ displayName: "brc-sc-AsideContainer", componentId: "brc-sc-v0x4ot" }) ` display: flex; flex: 1; justify-content: flex-end; margin-left: 8px; `; const RadioIndicator = styled.input.attrs(() => ({ type: 'radio' })).withConfig({ displayName: "brc-sc-RadioIndicator", componentId: "brc-sc-12jke03" }) ` box-sizing: border-box; border-radius: 50%; background-color: ${({ checked, isInvalid, theme }) => getIndicatorColor(theme, checked, isInvalid)}; height: 12px; width: 12px; appearance: none; margin: 0; font-size: inherit; transition: background-color 300ms ease; `; const RadioSpot = styled.div.withConfig({ displayName: "brc-sc-RadioSpot", componentId: "brc-sc-1bfnqqv" }) ` display: flex; justify-content: center; align-items: center; box-sizing: border-box; border-radius: 50%; border-style: solid; border-width: ${({ checked, theme }) => (checked && !isRebrand(theme) ? '2px' : '1px')}; border-color: ${({ disabled, checked, isInvalid, theme }) => getBorderColor(theme, disabled, isInvalid, checked)}; background: ${({ disabled, isInvalid, theme }) => getFillColor(theme, disabled, isInvalid)}; opacity: ${({ disabled, theme }) => disabled && isRebrand(theme) && '38%'}; height: 24px; flex: 0 0 auto; width: 24px; transition: box-shadow 300ms ease; &:focus-within { box-shadow: ${({ theme }) => isRebrand(theme) ? `0 0 0 4px ${theme.navStatusPositive500}` : `0 0 0 8px ${theme.bubbleBlue300}`}; & > ${RadioIndicator} { outline: none; } } `; export const Radio = (_a) => { var { asideContent, autoFocus, checkedBackgroundColor, children: _children, label, className, checked, disabled, hasBorder, isInvalid } = _a, props = __rest(_a, ["asideContent", "autoFocus", "checkedBackgroundColor", "children", "label", "className", "checked", "disabled", "hasBorder", "isInvalid"]); const inputRef = useRef(null); useEffect(() => { if (autoFocus && inputRef.current) { focusWithoutScroll(inputRef.current); } }, [autoFocus]); const Label = typeof label === 'string' ? CopyLabel : DivLabel; return (_jsxs(RadioItem, Object.assign({ asideContent: asideContent, checked: checked, className: className, hasBorder: hasBorder, checkedBackgroundColor: checkedBackgroundColor }, { children: [_jsx(RadioSpot, Object.assign({}, { checked, disabled, isInvalid }, { children: _jsx(RadioIndicator, Object.assign({ ref: inputRef }, { checked, disabled, isInvalid }, props)) })), _jsx(Label, Object.assign({ isInvalid: isInvalid }, { children: label })), asideContent && _jsx(AsideContainer, { children: asideContent })] }))); }; const StyledRadio = styled(Radio).withConfig({ displayName: "brc-sc-StyledRadio", componentId: "brc-sc-1eoo6of" }) ``; export default StyledRadio; //# sourceMappingURL=radio.js.map