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

Allow grabbing theme values with a decimal #815

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/core/lib/createTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ function createTheme(
defaultValue?: string,
options = {}
): number | boolean | Record<string, unknown> {
const [pathRoot, ...subPaths] = toPath(path)
let [pathRoot, ...subPaths] = toPath(path)

// Retain dots in spacing values, eg: `ml-[theme(spacing.0.5)]`
if (
pathRoot === 'spacing' &&
subPaths.length === 2 &&
subPaths.every(x => !Number.isNaN(Number(x)))
) {
subPaths = [subPaths.join('.')]
}

const value = getConfigValue(
path ? ['theme', pathRoot, ...subPaths] : ['theme'],
Expand Down
12 changes: 12 additions & 0 deletions tests/arbitraryValues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ it('should allow using the DEFAULT key when using theme', async () => {
})
})

it('should allow dots instead of square brackets for decimal point values', async () => {
const input = 'tw`ml-[theme(spacing.0.5)]`'

return run(input).then(result => {
expect(result).toMatchFormattedJavaScript(`
({
"marginLeft": "0.125rem"
});
`)
})
})

it('should not output unparsable arbitrary CSS values', async () => {
// eslint-disable-next-line no-template-curly-in-string
const input = 'tw`w-[${sizes.width}]`'
Expand Down