digdir / designsystemet

Designsystemet
https://designsystemet.no
MIT License
85 stars 38 forks source link

Give typesafety for `data-*` #2655

Closed Barsnes closed 1 week ago

Barsnes commented 1 month ago

We don't need to add this to our package, but we can tell our users how to ensure they have typesafety and autocomplete for our data attributes.

declare global {
  namespace React.JSX {
    interface IntrinsicAttributes {
      'data-color-scheme'?: 'dark' | 'light'
    }
  }
  namespace React {
    interface HTMLAttributes<T> {
      'data-color-scheme'?: 'dark' | 'light';
    }
  }
}

We already do this for popover (popovertarget), and this is just something we need to add to our docs

Same goes for size:

Since all components (and HTML elements) will support data-size and data-color, we should expose this as props also in React and thus avoiding confusion on when size is available and when it is not. We can however provide type safety by creating a shared props definition, which all components can extend:

/** Recommended sizes */
'data-size'?: 'sm' | 'md' | 'xl';

and those components with more sizes (Avatar, Spinner, Heading) can extend with more sizes in data-size

Note: data-color-scheme has been separated out to #2812

unekinn commented 1 month ago

My suggestion:

unekinn commented 3 weeks ago

I'm handling this as part of #2703.