Skip to content

Commit ca8fc5c

Browse files
committed
Merge branch '62-bind-problem-with-conditional-statements'
2 parents 6369a7c + 1687394 commit ca8fc5c

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Svelte preprocess CSS Modules, changelog
22

3+
## 2.2.3 (June 21, 2022)
4+
5+
### Fixes
6+
7+
- Add support for css binding on svelte blocks `{#if}` `{#each}` `{#await}` `{#key}` [issue 62](https://github.com/micantoine/svelte-preprocess-cssmodules/issues/62)
8+
39
## 2.2.2 (June 21, 2022)
410

511
### Fixes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-preprocess-cssmodules",
3-
"version": "2.2.2",
3+
"version": "2.2.3",
44
"description": "Svelte preprocessor to generate CSS Modules classname on Svelte components",
55
"keywords": [
66
"svelte",

src/parsers/template.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ const addDynamicVariablesToElements = (
5454
cssVar: CssVariables
5555
): void => {
5656
node.children?.forEach((childNode) => {
57-
if (childNode.type === 'InlineComponent') {
57+
if (
58+
childNode.type === 'InlineComponent' ||
59+
childNode.type === 'EachBlock' ||
60+
childNode.type === 'KeyBlock'
61+
) {
5862
addDynamicVariablesToElements(processor, childNode, cssVar);
5963
} else if (childNode.type === 'Element') {
6064
const attributesLength = childNode.attributes.length;
@@ -72,6 +76,13 @@ const addDynamicVariablesToElements = (
7276
` ${cssVar.styleAttribute}`
7377
);
7478
}
79+
} else if (childNode.type === 'IfBlock') {
80+
addDynamicVariablesToElements(processor, childNode, cssVar);
81+
addDynamicVariablesToElements(processor, childNode.else, cssVar);
82+
} else if (childNode.type === 'AwaitBlock') {
83+
addDynamicVariablesToElements(processor, childNode.pending, cssVar);
84+
addDynamicVariablesToElements(processor, childNode.then, cssVar);
85+
addDynamicVariablesToElements(processor, childNode.catch, cssVar);
7586
}
7687
});
7788
};

test/globalFixtures/bindVariable.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,92 @@ describe('Bind variable to CSS', () => {
113113
</style>`
114114
);
115115
});
116+
117+
test('root elements has if statement', async () => {
118+
const output = await compiler({
119+
source: `${script}` +
120+
`{#if color === 'blue'}<div>blue</div>` +
121+
`{:else if color === 'red'}<div>red</div>` +
122+
`{:else}<div>none</div>` +
123+
`{/if}<style module>div{color:bind(color)}</style>`,
124+
}, {
125+
cssVariableHash: '123',
126+
});
127+
128+
expect(output).toBe(
129+
`${script}` +
130+
`{#if color === 'blue'}<div style="--color-123:{color};">blue</div>` +
131+
`{:else if color === 'red'}<div style="--color-123:{color};">red</div>` +
132+
`{:else}<div style="--color-123:{color};">none</div>`+
133+
`{/if}<style module>:global(div){color:var(--color-123)}</style>`
134+
);
135+
});
136+
137+
test('root elements has `each` statement', async () => {
138+
const output = await compiler({
139+
source: `${script}` +
140+
`{#each [0,1,2,3] as number}` +
141+
`<div>{number}</div>` +
142+
`{/each}<style module>div{color:bind(color)}</style>`,
143+
}, {
144+
cssVariableHash: '123',
145+
});
146+
147+
expect(output).toBe(
148+
`${script}` +
149+
`{#each [0,1,2,3] as number}` +
150+
`<div style="--color-123:{color};">{number}</div>` +
151+
`{/each}<style module>:global(div){color:var(--color-123)}</style>`
152+
);
153+
});
154+
155+
test('root element has `each` statement', async () => {
156+
const output = await compiler({
157+
source: `${script}` +
158+
`{#await promise}` +
159+
`<p>...waiting</p>` +
160+
`{:then number}` +
161+
`<p>The number is {number}</p>` +
162+
`{:catch error}` +
163+
`<p>{error.message}</p>` +
164+
`{/await}` +
165+
`{#await promise then value}` +
166+
`<p>the value is {value}</p>` +
167+
`{/await}<style module>div{color:bind(color)}</style>`,
168+
}, {
169+
cssVariableHash: '123',
170+
});
171+
172+
expect(output).toBe(
173+
`${script}` +
174+
`{#await promise}` +
175+
`<p style="--color-123:{color};">...waiting</p>` +
176+
`{:then number}` +
177+
`<p style="--color-123:{color};">The number is {number}</p>` +
178+
`{:catch error}` +
179+
`<p style="--color-123:{color};">{error.message}</p>` +
180+
`{/await}` +
181+
`{#await promise then value}` +
182+
`<p style="--color-123:{color};">the value is {value}</p>` +
183+
`{/await}<style module>:global(div){color:var(--color-123)}</style>`
184+
);
185+
});
186+
187+
test('root element has `key` statement', async () => {
188+
const output = await compiler({
189+
source: `${script}` +
190+
`{#key value}` +
191+
`<div transition:fade>{value}</div>` +
192+
`{/key}<style module>div{color:bind(color)}</style>`,
193+
}, {
194+
cssVariableHash: '123',
195+
});
196+
197+
expect(output).toBe(
198+
`${script}` +
199+
`{#key value}` +
200+
`<div transition:fade style="--color-123:{color};">{value}</div>` +
201+
`{/key}<style module>:global(div){color:var(--color-123)}</style>`
202+
);
203+
});
116204
});

0 commit comments

Comments
 (0)