|
| 1 | +import { |
| 2 | + createMultiStyleConfigHelpers, |
| 3 | + cssVar, |
| 4 | + defineStyle, |
| 5 | + getToken, |
| 6 | +} from "@chakra-ui/react" |
| 7 | +import { avatarAnatomy } from "@chakra-ui/anatomy" |
| 8 | +import { avatarDefaultTheme, defineMergeStyles } from "./components.utils" |
| 9 | +import { pick } from "lodash" |
| 10 | + |
| 11 | +const { defineMultiStyleConfig, definePartsStyle } = |
| 12 | + createMultiStyleConfigHelpers(avatarAnatomy.keys) |
| 13 | + |
| 14 | +const { baseStyle: defaultBaseStyle, sizes: defaultSizes } = avatarDefaultTheme |
| 15 | + |
| 16 | +const $border = cssVar("avatar-border-color", "transparent") |
| 17 | +const $mlBySize = cssVar("ml-by-size") |
| 18 | + |
| 19 | +const baseStyleContainer = defineStyle((props) => { |
| 20 | + const primaryLowContrast = getToken( |
| 21 | + "colors", |
| 22 | + "primary.lowContrast" |
| 23 | + )(props.theme) |
| 24 | + |
| 25 | + return defineMergeStyles(defaultBaseStyle?.(props).container, { |
| 26 | + [$border.variable]: "transparent", |
| 27 | + borderWidth: "1px", |
| 28 | + "&:hover, [data-peer]:hover ~ &": { |
| 29 | + boxShadow: `0.15em 0.15em 0 ${primaryLowContrast}`, |
| 30 | + }, |
| 31 | + _focus: { |
| 32 | + outline: "4px solid", |
| 33 | + outlineColor: "primary.hover", |
| 34 | + outlineOffset: "-1px", |
| 35 | + }, |
| 36 | + _active: { |
| 37 | + [$border.variable]: "colors.primary.hover", |
| 38 | + boxShadow: "none", |
| 39 | + "& img": { |
| 40 | + opacity: 0.7, |
| 41 | + }, |
| 42 | + }, |
| 43 | + "[role='group'] &": { |
| 44 | + [$border.variable]: "colors.background.base", |
| 45 | + _notLast: { |
| 46 | + marginLeft: $mlBySize.reference, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +const baseStyleExessLabel = defineStyle((props) => |
| 53 | + defineMergeStyles(defaultBaseStyle?.(props).excessLabel, { |
| 54 | + bg: "body.base", |
| 55 | + color: "background.base", |
| 56 | + ms: $mlBySize.reference, |
| 57 | + }) |
| 58 | +) |
| 59 | + |
| 60 | +const baseStyle = definePartsStyle((props) => ({ |
| 61 | + container: baseStyleContainer(props), |
| 62 | + excessLabel: baseStyleExessLabel(props), |
| 63 | +})) |
| 64 | + |
| 65 | +const USED_SIZES = ["xs", "sm", "md", "lg"] as const |
| 66 | + |
| 67 | +const pickedDefaultSizes: { [k in (typeof USED_SIZES)[number]]?: object } = |
| 68 | + pick(defaultSizes, ...USED_SIZES) |
| 69 | + |
| 70 | +const sizes = defineMergeStyles(pickedDefaultSizes, { |
| 71 | + xs: { |
| 72 | + group: { |
| 73 | + [$mlBySize.variable]: "space.-1", |
| 74 | + }, |
| 75 | + excessLabel: { |
| 76 | + fontSize: "0.563rem", |
| 77 | + }, |
| 78 | + }, |
| 79 | + sm: { |
| 80 | + group: { |
| 81 | + [$mlBySize.variable]: "space.-2", |
| 82 | + }, |
| 83 | + excessLabel: { |
| 84 | + fontSize: "sm", |
| 85 | + }, |
| 86 | + }, |
| 87 | +}) |
| 88 | + |
| 89 | +export const Avatar = defineMultiStyleConfig({ |
| 90 | + baseStyle, |
| 91 | + // @ts-expect-error |
| 92 | + sizes, |
| 93 | +}) |
0 commit comments