-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 for custom theme types via TypeScript module augmentation #5579
Support for custom theme types via TypeScript module augmentation #5579
Conversation
🦋 Changeset detectedLatest commit: 3c3ca6b The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/chakra-ui/chakra-ui-storybook/3PbTC6znSpnVCyvXLJN8soW6RPFJ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 3c3ca6b:
|
bac2b3b
to
f0194cb
Compare
f0194cb
to
b00f403
Compare
Oops, I forgot a changeset. I added one and updated the inline documentation to extend Thank you! |
Love this PR! 💖 I'll review it over the weekend, because I have currently only access to my phone.. I am excited! 🎉 |
Hi @jrolfs, I just got autocomplete from the default theme and not the augmented types (IDEA nor VSCode). Can you kindly give me hint how to test this?
|
I just pulled down your repo and linked in my existing Chakra branch and it seems to be working in Code. I'll dig in a bit more tomorrow and make sure I didn't forget something in the PR. I was hoping to be able to just set up a CodeSandbox with the PR releases to test and demo this, but now that I look I don't see those happening on PRs anymore? I thought every PR got released on the PR dist-tag? |
Here's an example of the PR releases I was talking about: #5290 (comment) |
b00f403
to
923ae85
Compare
Aha, I forgot to export |
923ae85
to
3c3ca6b
Compare
Awesome! I'll try it this evening.
The PR releases were only available for core members until I disabled them yesterday completely, because they had a bug where we released some packages unwillingly without an explicit release and without a pr dist tag 😬 Releasing unreviewed code in the chakra npm namespace raises some security concerns. |
Thanks, that worked great! ✨ I tinkered a bit with the module augmentation and came up with this question: The following module augmentation works in my test repository with the current version of chakra ui: type DefaultSizes = 'small' | 'medium' | 'large';
declare module "@chakra-ui/styled-system" {
export interface ThemeTypings {
borders: 'none' | 'thin' | 'thick';
components: {
Button: {
sizes: DefaultSizes;
variants: 'wacky' | 'chill';
};
}
}
} Why do we need to introduce a new interface In my current understanding the only way to override the augmented types from the "on-top-chakra component lib" for the user is to use augmented types again. graph TD
Chakra[chakra default ThemeTypings] -->|via CLI| CompLib[on-top-chakra component lib]
CompLib -->|via TS Module Augmentation| App
App -->|ONLY possible via TS Module Augmentation| App
You mentioned that we could add a feature to the CLI to generate the augmented type definitions:
And I think this is a great idea! And I think this would make this PR obsolete 🤔 Kindly let me know if I am missing something! |
Sorry, I guess I should taken notes back when I started trying to only leverage module augmentation to accomplish this. Going from your latest commit, it looks like component/theme props ( When we don't import when we do import it, the default types take precedence: Maybe we can modify the |
True that! Thank you for laying our your thought process, I did learn a lot 💖 IMO this is good to go as is 🚀 This feature implicates a few follow up steps:
✨ Awesome work @jrolfs! |
Hey, @TimKolberger thank you so much for testing this and for the feedback! I meant to at least acknowledge your concerns illustrated with that pretty diagram ;) — I think for this particular use case that further type customization at the application level won't really be a thing, but I do think it's important that we make that limitation clear...
as, yes, once |
It was a pleasure collaborating with you on that topic and I am looking forward to further ideas and PRs 💖 Yes, the mentioned edge case is rare and we will never know what chakra users will experience in their codebase 😄 if we document it, maybe someone will save a few hours of debugging. Cheers |
📝 Description
Here I'm following up on a previous idea I had mentioned in this PR. The TypeScript experience when distributing a component library based on Chakra still isn't great on account of the Chakra CLI step being required for consumers to the library to get the proper language server/editor experience. Not only is it an extra step, but there are multiple ways in which it can break preventing consumers of our library from getting accurate type definitions.
In this PR, I'm proposing a small change to the way that these custom types are defined that both allow the existing CLI functionality to continue working exactly as it had previously, but also providing an optional advanced feature that would allow custom type definitions to be shipped with component libraries that are based on Chakra that would not require end users to install and run the CLI. It also makes it possible to manually customize the type definitions, if desired.
⛳️ Current behavior (updates)
Defining custom theme typings relies on the @chakra-ui/cli package always running and overwriting the
ThemeTypings
interface in the end users'./node_modules/
directory as documented here.🚀 New behavior
This PR makes it possible to also customize the
ThemeTypings
interface using TypeScript's module augmentation and declaration merging features, much like custom theme types are implemented in Styled Components and Emotion.In order to take advantage of this approach, the
CustomThemeTypings
interface needs to be "augmented" and at least implement theDefaultThemeTypings
interface, otherwise it will fall back to the existingThemeTypings
interface that the CLI overwrites.Example
types/chakra.d.ts
:💣 Is this a breaking change (Yes/No):
No, see above.
📝 Additional Information
In it's current form, this PR simply makes this possible and adds some rudimentary inline documentation. I think it would be reasonable (barring maybe some feedback on how I named things) to merge this PR as is. With that said, I am very much ready to add documentation explaining the feature and I was even thinking we could combine this approach with the CLI with a new option to ouput a version of the definitions that leverages the above approach as opposed to modifying the definitions directly in
./node_modules
.In addition to making it possible to include custom type definitions with a component library that's based on Chakra, I think avoiding messing around in
./node_modules/
has some pretty clear benefits. Let me know what y'all think!