@navinc/base-react-components
Version:
Nav's Pattern Library
123 lines (119 loc) • 4.93 kB
JavaScript
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';
const getLabelStyling = ({ isInvalid, theme }) => `
margin: 0;
padding: 0;
color: ${isInvalid ? theme.error : 'inherit'};
`;
const getFillColor = (theme, disabled, isInvalid) => {
return isInvalid ? theme.navStatusNegative100 : theme.navNeutralLight;
};
const getBorderColor = (theme, disabled, isInvalid, checked) => {
if (isInvalid)
return theme.navStatusNegative;
return checked ? theme.navPrimary400 : theme.navNeutral300;
};
const getIndicatorColor = (theme, checked, isInvalid) => {
if (checked)
return isInvalid ? theme.navStatusNegative : theme.navPrimary400;
return 'transparent';
};
const CopyLabel = styled(Copy).withConfig({
shouldForwardProp: (prop) => !['isInvalid'].includes(prop),
}) `
${getLabelStyling}
`;
const DivLabel = styled.div.withConfig({ displayName: "brc-sc-DivLabel", componentId: "brc-sc-129p17w" }) `
${getLabelStyling}
`;
const RadioItem = styled.label.withConfig({ displayName: "brc-sc-RadioItem", componentId: "brc-sc-exqvsd" }) `
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: 8px;
border: 1px solid ${theme.navNeutral300};
padding: 14px 16px;
min-height: 24px;
`}
${({ 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-1z0xg80" }) `
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-oknnwc" }) `
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-13p9x1i" }) `
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
border-radius: 50%;
border-style: solid;
border-width: 1px;
border-color: ${({ disabled, checked, isInvalid, theme }) => getBorderColor(theme, disabled, isInvalid, checked)};
background: ${({ disabled, isInvalid, theme }) => getFillColor(theme, disabled, isInvalid)};
opacity: ${({ disabled }) => disabled && '38%'};
height: 24px;
flex: 0 0 auto;
width: 24px;
transition: box-shadow 300ms ease;
&:focus-within {
box-shadow: 0 0 0 4px ${({ theme }) => theme.navStatusPositive500};
& > ${RadioIndicator} {
outline: none;
}
}
`;
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, { asideContent: asideContent, checked: checked, className: className, hasBorder: hasBorder, checkedBackgroundColor: checkedBackgroundColor, children: [_jsx(RadioSpot, { checked, disabled, isInvalid, children: _jsx(RadioIndicator, Object.assign({ ref: inputRef, checked, disabled, isInvalid }, props)) }), _jsx(Label, { isInvalid: isInvalid, children: label }), asideContent && _jsx(AsideContainer, { children: asideContent })] }));
};
export const Radio = styled(_Radio).withConfig({ displayName: "brc-sc-Radio", componentId: "brc-sc-1s0mpl7" }) ``;
//# sourceMappingURL=radio.js.map