-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathGroupBox.tsx
More file actions
69 lines (62 loc) · 1.82 KB
/
Copy pathGroupBox.tsx
File metadata and controls
69 lines (62 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { forwardRef } from 'react';
import styled, { css } from 'styled-components';
import { createDisabledTextStyles } from '../common';
import { CommonStyledProps } from '../types';
type GroupBoxProps = {
label?: React.ReactNode;
children?: React.ReactNode;
disabled?: boolean;
variant?: 'default' | 'flat';
} & React.FieldsetHTMLAttributes<HTMLFieldSetElement> &
CommonStyledProps;
const StyledFieldset = styled.fieldset<
Pick<GroupBoxProps, 'variant'> & { $disabled: boolean }
>`
position: relative;
border: 2px solid
${({ theme, variant }) =>
variant === 'flat' ? theme.flatDark : theme.borderLightest};
padding: 16px;
margin-top: 8px;
font-size: 1rem;
color: ${({ theme }) => theme.materialText};
${({ variant }) =>
variant !== 'flat' &&
css`
box-shadow: -1px -1px 0 1px ${({ theme }) => theme.borderDark},
inset -1px -1px 0 1px ${({ theme }) => theme.borderDark};
`}
${props => props.$disabled && createDisabledTextStyles()}
`;
const StyledLegend = styled.legend<Pick<GroupBoxProps, 'variant'>>`
display: flex;
position: absolute;
top: 0;
left: 8px;
transform: translateY(calc(-50% - 2px));
padding: 0 8px;
font-size: 1rem;
background: ${({ theme, variant }) =>
variant === 'flat' ? theme.canvas : theme.material};
`;
const GroupBox = forwardRef<HTMLFieldSetElement, GroupBoxProps>(
(
{ label, disabled = false, variant = 'default', children, ...otherProps },
ref
) => {
return (
<StyledFieldset
aria-disabled={disabled}
$disabled={disabled}
variant={variant}
ref={ref}
{...otherProps}
>
{label && <StyledLegend variant={variant}>{label}</StyledLegend>}
{children}
</StyledFieldset>
);
}
);
GroupBox.displayName = 'GroupBox';
export { GroupBox, GroupBoxProps };