Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom theme types in TS bindings #1228

Open
ersinakinci opened this issue Oct 29, 2020 · 2 comments
Open

Support custom theme types in TS bindings #1228

ersinakinci opened this issue Oct 29, 2020 · 2 comments
Labels

Comments

@ersinakinci
Copy link

Is your feature request related to a problem? Please describe.
When using the sx prop, the useThemeUI hook, or any other API that exposes the theme object, it would be really nice to have a way to set a custom theme type for that object on a project-wide basis. So when I write

<div sx={{ backgroundImage: ({ colors: { red, blue } }) => `linear-gradient(to right, red, blue)` }} />

tsc shouldn't throw an error, which it currently does because the default Theme type's properties are all optional (so the compiler has no way of knowing whether colors exists on the theme object).

Describe the solution you'd like
We should somehow be able to use generics to solve this problem:

  1. Leave out the ambient type that extends JSX elements with the sx prop. Explain how to extend the JSX interface with your own theme type in the docs. It should look something like this (whereas SxProps isn't currently generic):
import { SxProps } from '@theme-ui/core';

declare module 'react' {
  interface DOMAttributes<T> extends SxProps<MyTheme> {}
}
declare global {
  namespace JSX {
    interface IntrinsicAttributes extends SxProps<MyTheme> {}
  }
}
  1. Rewrite the useThemeUI hook in a way that takes advantage of a generic type and explain how to use this feature in the docs. Something like:
import { useThemeUI } from 'theme-ui';

export const useTheme = useThemeUI<MyTheme>;
  1. Repeat step 2 for all the other functions/API's in the Theme UI ecosystem that are currently hardwired to use the non-generic Theme type. For example, @theme-ui/color.

Describe alternatives you've considered
I developed a brute-force workaround by disabling Theme UI's types in tsconfig.json and manually redefining the ones that define the theme object and related API's.

Additional context
Original issue described here: #1188 (comment)

@ersinakinci
Copy link
Author

Note that my proposed solution is an opt-in approach. The generic types should all fall back to the default Theme type, both to preserve current behavior and to allow for correct typing in scenarios where you genuinely don't know what properties will be available on a theme object. The latter may occur, for example, when using unvalidated, user-authored themes.

@hasparus
Copy link
Member

hasparus commented Nov 2, 2020

Connected to #1090.

@lachlanjc lachlanjc added the types label Dec 2, 2020
@lachlanjc lachlanjc linked a pull request Dec 6, 2020 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants