Skip to content

Commit

Permalink
feat: allow customizing sizes. DO NOT MERGE - test-custom-sizes.d.ts …
Browse files Browse the repository at this point in the history
…just exists to prove it works
  • Loading branch information
unekinn committed Jan 22, 2025
1 parent c7b5d47 commit 0273a92
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 12 deletions.
11 changes: 11 additions & 0 deletions apps/storybook/test-custom-sizes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {} from '@digdir/designsystemet-react/sizes';

declare module '@digdir/designsystemet-react/sizes' {
export interface SizeMap {
'2xs': never;
xs: never;
sm: 'small';
md: 'medium';
lg: 'laaaarge';
}
}
5 changes: 4 additions & 1 deletion apps/storybook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"types": ["../../packages/theme/brand/colors.d.ts"]
"types": [
"./test-custom-sizes.d.ts",
"../../packages/theme/brand/colors.d.ts"
]
},
"include": [".", "./.storybook/**/*", "../../packages/css/postcss.config.js"]
}
2 changes: 1 addition & 1 deletion packages/react/react-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Size } from '@digdir/designsystemet-react';
import type { Color } from '@digdir/designsystemet-react/colors';
import type { Size } from '@digdir/designsystemet-react/sizes';

declare global {
namespace React {
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Slot } from '@radix-ui/react-slot';
import cl from 'clsx/lite';
import { Fragment, forwardRef } from 'react';
import type { HTMLAttributes, ReactNode } from 'react';
import type { DefaultProps, Size } from '../../types';
import type { GetSizes, Size } from '../../sizes';
import type { DefaultProps } from '../../types';
import type { MergeRight } from '../../utilities';

export type AvatarProps = MergeRight<
Expand All @@ -15,7 +16,7 @@ export type AvatarProps = MergeRight<
/**
* The size of the avatar.
*/
'data-size'?: 'xs' | Size;
'data-size'?: GetSizes<'xs'> | Size;
/**
* The shape of the avatar.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cl from 'clsx/lite';
import { useContext, useId } from 'react';
import type { HTMLAttributes, InputHTMLAttributes, ReactNode } from 'react';

import type { Size } from '../../../types';
import type { Size } from '../../../sizes';
import { FieldsetContext } from './FieldsetContext';

export type FormFieldProps = {
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/Heading/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Slot } from '@radix-ui/react-slot';
import cl from 'clsx/lite';
import type { ElementType, HTMLAttributes } from 'react';
import { forwardRef } from 'react';
import type { AllPossibleSizes, GetSizes } from '../../sizes';

export type HeadingProps = {
/**
Expand All @@ -11,7 +12,7 @@ export type HeadingProps = {
level?: 1 | 2 | 3 | 4 | 5 | 6;
/** Changes text sizing
*/
'data-size'?: '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
'data-size'?: GetSizes<AllPossibleSizes>;
/**
* Change the default rendered element for the one passed as a child, merging their props and behavior.
* @default false
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/Paragraph/Paragraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Slot } from '@radix-ui/react-slot';
import cl from 'clsx/lite';
import type { HTMLAttributes } from 'react';
import { forwardRef } from 'react';
import type { GetSizes } from '../../sizes';

export type ParagraphProps = {
/**
* Changes text sizing
*/
'data-size'?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
'data-size'?: GetSizes<'xs' | 'sm' | 'md' | 'lg' | 'xl'>;
/**
* Adjusts styling for paragraph length
* @default 'default'
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMergeRefs } from '@floating-ui/react';
import cl from 'clsx/lite';
import { type ComponentPropsWithoutRef, forwardRef } from 'react';
import type { GetSizes } from '../../sizes';
import { useSynchronizedAnimation } from '../../utilities';

export type SpinnerProps = {
Expand All @@ -9,7 +10,7 @@ export type SpinnerProps = {
/**
* Spinner size
*/
'data-size'?: '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl';
'data-size'?: GetSizes<'2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl'>;
} & ComponentPropsWithoutRef<'svg'> &
(
| { 'aria-label': string; 'aria-hidden'?: never }
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './components';
export * from './utilities';
export type { Size } from './types';
export type { Size } from './sizes';
export type { Color } from './colors';
38 changes: 38 additions & 0 deletions packages/react/src/sizes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Base interface for user-defined mappings from Designsystemet's internal size names
* to user-defined sizes. This allows a downstream library consumer to augment this
* interface to change or disable a given size.
*
* Why would you do this?
* - It lets you change the sizing scale (e.g. map all sizes one size down)
* - It allows you to rename the sizes as you please (e.g. "small", "medium", ...)
* NB: This means you would have to bring your own `data-size` styling, as renaming
* a size to one that doesn't already exist in Designsystemet opts out of the
* built-in `data-size` styling.
* @example
* import type {} from '@digdir/designsystemet-react/sizes';
*
* // This will disable '2xs' and rename 'sm' to 'small'
* declare module '@digdir/designsystemet-react/sizes' {
* export interface SizeMap {
* '2xs': never;
* sm: 'small';
* }
* }
*
*/
// biome-ignore lint/suspicious/noEmptyInterface: used for interface augmentation
export interface SizeMap {}

export type AllPossibleSizes = '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';

type DefaultSizeMap = {
[K in AllPossibleSizes]: K;
};

type CombinedSizeMap = Omit<DefaultSizeMap, keyof SizeMap> & SizeMap;

/** Look up the actual size names for the given internal size names */
export type GetSizes<T extends AllPossibleSizes> = CombinedSizeMap[T];

export type Size = GetSizes<'sm' | 'md' | 'lg'>;
3 changes: 1 addition & 2 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ReactNode } from 'react';
import type { Color } from './colors';

export type Size = 'sm' | 'md' | 'lg';
import type { Size } from './sizes';

export type DefaultProps = {
/**
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"packages/*": ["this/dir/does/not/exist/*"],
// Paths matching package.json exports section in @digdir/designsystem-react
"@digdir/designsystemet-react": ["packages/react/src/index.ts"],
"@digdir/designsystemet-react/colors": ["packages/react/src/colors.ts"]
"@digdir/designsystemet-react/colors": ["packages/react/src/colors.ts"],
"@digdir/designsystemet-react/sizes": ["packages/react/src/sizes.ts"],
},
"target": "ES2022",
"module": "ESNext",
Expand Down

0 comments on commit 0273a92

Please sign in to comment.