-
-
Notifications
You must be signed in to change notification settings - Fork 184
Give theme import access to the whole theme #169
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
Conversation
|
The typing for the theme function currently requires you to convert to const sm = theme('screens.sm') as unknown as string;It might be nice to update the type for theme to support a generic type to simplify the return type: const sm = theme<string>('screens.sm');What do people think? I think this change could be nice as it is a non-breaking change (backwards compatible) and will simplify the code. |
|
Makes sense @acidic9, I'll squeeze it into this PR 👍 |
|
Hey @acidic9 👋 Here's the type I've arrived on: export type ThemeFn = <T>(arg?: unknown) => string;If you get some time, could you please check if this covers your use case? I've tested with these cases: import { theme } from "twin.macro";
(theme("screens.sm") as unknown) as string;
(theme("") as unknown) as string;
(theme() as unknown) as string;
theme("screens.sm") as string;
theme("") as string;
theme() as string;
theme<string>("screens.sm");
theme<string>(`screens.sm`);
theme<string>`screens.sm`;
theme<string>`screens.sm`;
theme<string>``;
theme<string>(``);
theme<string>("");
theme<string>();
theme("screens.sm");
theme`screens.sm`;
theme(`screens.sm`);
theme();
theme("");
theme``;If all is good, I'll merge this in on Wednesday this week. |
|
Hi @ben-rogerson thanks for doing this! Really appreciate it. In your theme fn type: export type ThemeFn = <T>(arg?: unknown) => string;I noticed that the As the theme function has been updated to support returning any part of the theme, the return type could be anything from I was thinking the theme function type might be something like: export type ThemeFn = <T = string>(arg?: string) => T;This ensures the argument is a string, and defaults the return type to The following would be the case in this situation: const screenMd = theme('screens.md') // string
const screenLg = theme<number>('screens.lg') // number
const screens = theme<Record<string, string>>('screens') // { key: 'value', ... } |
|
Thanks for the help here 👍 I'm testing that type and seeing some issues with template literals which I need to support also: export type ThemeFn = <T = string>(arg?: string) => T;I wonder if we can support template literals too? |
|
I see, I think it's as simple as: export type ThemeFn = <T = string>(arg?: string | TemplateStringsArray) => T; |
|
Looks like that fixed it :) |
29616b8 to
baffb3b
Compare
baffb3b to
fa1f0df
Compare

Previously the
themeimport could only grab string values from your merged tailwind config.This PR beefs up the theme import so you can now grab anything from the theme key of the merged tailwind.config.js
Some usage examples:
Grab the breakpoints
Once you've got the breakpoints, you could use them to create breakpoint helpers of your own.
Grab a set of colors
This is most likely to help you create custom themes on your site or app.
Grab the whole merged config
You can also grab the whole merged config with an empty theme call.
The theme call will be replaced with the entire merged config so beware of the bloat this will add to your file: