Skip to content

Commit c9afcbe

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

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,35 @@ 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;
163+
--spacing-3\.5: 3.5px;
164+
--spacing-3_5: 3.5px;
162165
--spacing-foo\/bar: 3rem;
163166
}
164167
@tailwind utilities;
165168
`,
166-
['m-1.5', 'm-foo/bar'],
169+
['m-1.5', 'm-2.5', 'm-2_5', 'm-3.5', 'm-foo/bar'],
167170
),
168171
).toMatchInlineSnapshot(`
169172
":root, :host {
170-
--spacing-1_5: 2.5rem;
173+
--spacing-1\\.5: 1.5px;
174+
--spacing-2_5: 2.5px;
175+
--spacing-3\\.5: 3.5px;
176+
--spacing-3_5: 3.5px;
171177
--spacing-foo\\/bar: 3rem;
172178
}
173179
174180
.m-1\\.5 {
175-
margin: var(--spacing-1_5);
181+
margin: var(--spacing-1\\.5);
182+
}
183+
184+
.m-2\\.5, .m-2_5 {
185+
margin: var(--spacing-2_5);
186+
}
187+
188+
.m-3\\.5 {
189+
margin: var(--spacing-3\\.5);
176190
}
177191
178192
.m-foo\\/bar {

packages/tailwindcss/src/theme.ts

Lines changed: 13 additions & 6 deletions
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)