Skip to content

Commit 8c193dc

Browse files
Allow both escaped dots and underscores to be registered in theme
1 parent e9c7140 commit 8c193dc

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

packages/tailwindcss/src/compat/plugin-api.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ describe('theme', async () => {
14251425
:root, :host {
14261426
--width-1: 0.25rem;
14271427
--width-1\\/2: 60%;
1428-
--width-1_5: 0.375rem;
1428+
--width-1\\.5: 0.375rem;
14291429
--width-2_5: 0.625rem;
14301430
}
14311431
"
@@ -1482,7 +1482,7 @@ describe('theme', async () => {
14821482
:root, :host {
14831483
--width-1: 0.25rem;
14841484
--width-1\\/2: 60%;
1485-
--width-1_5: 0.375rem;
1485+
--width-1\\.5: 0.375rem;
14861486
--width-2_5: 0.625rem;
14871487
}
14881488
"

packages/tailwindcss/src/index.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,27 @@ describe('compiling CSS', () => {
158158
css`
159159
@theme {
160160
--spacing-*: initial;
161-
--spacing-1\.5: 2.5rem;
161+
--spacing-1\.5: 1.5px;
162+
--spacing-2_5: 2.5px;
162163
--spacing-foo\/bar: 3rem;
163164
}
164165
@tailwind utilities;
165166
`,
166-
['m-1.5', 'm-foo/bar'],
167+
['m-1.5', 'm-2.5', 'm-2_5', 'm-foo/bar'],
167168
),
168169
).toMatchInlineSnapshot(`
169170
":root, :host {
170-
--spacing-1_5: 2.5rem;
171+
--spacing-1\\.5: 1.5px;
172+
--spacing-2_5: 2.5px;
171173
--spacing-foo\\/bar: 3rem;
172174
}
173175
174176
.m-1\\.5 {
175-
margin: var(--spacing-1_5);
177+
margin: var(--spacing-1\\.5);
178+
}
179+
180+
.m-2\\.5, .m-2_5 {
181+
margin: var(--spacing-2_5);
176182
}
177183
178184
.m-foo\\/bar {

packages/tailwindcss/src/theme.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ export class Theme {
4141
) {}
4242

4343
add(key: string, value: string, options = ThemeOptions.NONE): void {
44-
key = key.replaceAll('.', '_')
45-
4644
if (key.endsWith('-*')) {
4745
if (value !== 'initial') {
4846
throw new Error(`Invalid theme value \`${value}\` for namespace \`${key}\``)
@@ -147,11 +145,20 @@ export class Theme {
147145
#resolveKey(candidateValue: string | null, themeKeys: ThemeKey[]): string | null {
148146
for (let namespace of themeKeys) {
149147
let themeKey =
150-
candidateValue !== null
151-
? (`${namespace}-${candidateValue.replaceAll('.', '_')}` as ThemeKey)
152-
: namespace
148+
candidateValue !== null ? (`${namespace}-${candidateValue}` as ThemeKey) : namespace
149+
150+
if (!this.values.has(themeKey)) {
151+
// If the exact theme key is not found, we might be trying to resolve a key containing a dot
152+
// that was registered with an underscore instead:
153+
if (candidateValue !== null && candidateValue.includes('.')) {
154+
themeKey = `${namespace}-${candidateValue.replaceAll('.', '_')}` as ThemeKey
155+
156+
if (!this.values.has(themeKey)) continue
157+
} else {
158+
continue
159+
}
160+
}
153161

154-
if (!this.values.has(themeKey)) continue
155162
if (isIgnoredThemeKey(themeKey, namespace)) continue
156163

157164
return themeKey

0 commit comments

Comments
 (0)