From 565885d7c1f4bf036b6a5998a164af111dcc0f82 Mon Sep 17 00:00:00 2001 From: Jesse Pinho Date: Fri, 16 Aug 2024 19:56:02 -0700 Subject: [PATCH] Add better docs for density --- packages/ui/src/Density/index.tsx | 65 +++++++++++++++++++++++ packages/ui/src/hooks/useDensity/index.ts | 5 ++ 2 files changed, 70 insertions(+) diff --git a/packages/ui/src/Density/index.tsx b/packages/ui/src/Density/index.tsx index 7575a2cb9..0803a6494 100644 --- a/packages/ui/src/Density/index.tsx +++ b/packages/ui/src/Density/index.tsx @@ -8,6 +8,71 @@ export type DensityProps = { ? { sparse: true; compact?: never } : { compact: true; sparse?: never }); +/** + * Use the `` component to set the density for all descendants in the + * component tree that support density variants. + * + * In Penumbra UI, density is never set as a prop directly on a component. + * Instead, it's set indirectly via a React context, so that entire regions of a + * UI can have a matching density. + * + * For example, imagine you have a `` (which supports density + * variants), which contain a bunch of ``s (which also + * support density variants). You may also have other components in the table + * which contain nested components with density variants. If we used a `density` + * prop, you'd need to set that prop on every single component in that tree. + * + * Instead, you can simply wrap the entire `
` with `` + * or ``, and it will set a density context value for all + * descendant components: + * + * ```tsx + * + *
+ * + * + * This will be rendered with compact spacing. + * + * + *
+ *
+ * ``` + * + * Components that support density variants are recognizable because the use the + * `useDensity()` hook, and then style their elements based on the value they + * receive from that hook: + * + * ```tsx + * const SomeStyledComponent = styled.div<{ $density: Density }>` + * padding: ${props => props.theme.spacing(props.$density === 'sparse' ? 4 : 2)}; + * ` + * + * const MyComponent = () => { + * const density = useDensity(); + * + * return + * } + * ``` + * + * In some specific situations, you may want to make sure that a given component + * that supports density variants always is rendered at a specific density. For + * example, let's say you have an icon-only button as the `startAdornment` for a + * ``, and you want to make sure that icon-only button always + * renders as `compact` density. In that case, simply wrap the button in + * ``. Then, it will always be compact, even if there's a + * higher-up ``: + * + * ```tsx + * + * + * + * } + * /> + * ``` + */ export const Density = ({ children, sparse, diff --git a/packages/ui/src/hooks/useDensity/index.ts b/packages/ui/src/hooks/useDensity/index.ts index b7d655ef3..d3b0a7d31 100644 --- a/packages/ui/src/hooks/useDensity/index.ts +++ b/packages/ui/src/hooks/useDensity/index.ts @@ -8,8 +8,13 @@ import { DensityContext } from '../../utils/DensityContext'; * * @example * ```tsx + * const SomeStyledComponent = styled.div<{ $density: Density }>` + * padding: ${props => props.theme.spacing(props.$density === 'sparse' ? 4 : 2)}; + * ` + * * const MyComponent = () => { * const density = useDensity(); + * * return * } * ```