Skip to content

Commit bd36033

Browse files
committed
fix: For when the block-alias is the same name as a generated className.
- instead of using the generated className, we generate a unique name for that cssClass
1 parent 5517d72 commit bd36033

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

packages/@css-blocks/core/src/BlockTree/BlockClass.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,14 @@ export class BlockClass extends Style<BlockClass, Block, Block, Attribute> {
207207
public cssClass(config: ResolvedConfiguration): string {
208208
switch (config.outputMode) {
209209
case OutputMode.BEM:
210-
return this.isRoot ? `${this.block.name}` : `${this.block.name}__${this.name}`;
210+
let bemName = this.isRoot ? `${this.block.name}` : `${this.block.name}__${this.name}`;
211+
// if the generated name exists as an alias on the block, then generate a
212+
// unique name instead
213+
if (this.block.getAllStyleAliases().has(bemName)) {
214+
return this.isRoot ? `${this.block.name}_${this.block.guid}` : `${this.block.name}_${this.block.guid}__${this.name}`;
215+
} else {
216+
return bemName;
217+
}
211218
case OutputMode.BEM_UNIQUE:
212219
return this.isRoot ? `${this.block.name}_${this.block.guid}` : `${this.block.name}_${this.block.guid}__${this.name}`;
213220
default:

packages/@css-blocks/glimmer/test/fixtures/styled-app/src/ui/components/with-dynamic-classes/stylesheet.css

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
@block t from "./typography.css";
33

44
:scope {
5+
block-alias: my-scope-alias stylesheet__world;
56
color: red;
67
}
78

89
.world {
10+
block-alias: stylesheet__world--thick;
911
border: 1px solid black;
1012
}
1113

packages/@css-blocks/glimmer/test/template-rewrite-test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function pipeline(projectDir: string, analyzer: GlimmerAnalyzer, entry: st
5959

6060
describe("Template Rewriting", function() {
6161

62-
it("rewrites styles from dynamic attributes", async function() {
62+
it("rewrites styles from dynamic attributes with block aliases", async function() {
6363
let projectDir = fixture("styled-app");
6464
let analyzer = new GlimmerAnalyzer({}, {}, moduleConfig);
6565
let templatePath = fixture("styled-app/src/ui/components/with-dynamic-states/template.hbs");
@@ -82,17 +82,17 @@ describe("Template Rewriting", function() {
8282
);
8383
});
8484

85-
it("rewrites styles from dynamic classes", async function() {
85+
it("rewrites styles from dynamic classes. Also doesn't error when the block alias has the same className as that of a generated style", async function() {
8686
let projectDir = fixture("styled-app");
8787
let analyzer = new GlimmerAnalyzer({}, {}, moduleConfig);
8888
let templatePath = fixture("styled-app/src/ui/components/with-dynamic-classes/template.hbs");
8989
let result = await pipeline(projectDir, analyzer, "with-dynamic-classes", templatePath);
9090
assert.deepEqual(minify(print(result.ast)), minify(`
91-
<div class="a">
92-
<h1 class="d">Hello, <span class="e h {{-css-blocks-classnames 3 4 0 isWorld 1 2 0 3 1 2 (eq isThick 1) 1 3 4 2 1 textStyle "bold" 1 0 "italic" 1 1 "f" 0 "g" 1 "b" 2 "c" 3}}">World</span>!</h1>
93-
<div class={{-css-blocks-classnames 1 2 0 isWorld 1 1 1 0 "e" 0 "b" 1}}>World</div>
94-
<div class={{-css-blocks-classnames 1 2 0 isWorld 1 0 1 1 "e" 0 "b" 1}}>World</div>
95-
<div class={{-css-blocks-classnames 1 1 0 isWorld 0 1 0 "b" 0}}>World</div>
91+
<div class="my-scope-alias a stylesheet__world">
92+
<h1 class="d">Hello, <span class="e h {{-css-blocks-classnames 3 5 0 isWorld 1 4 0 3 1 4 (eq isThick 1) 1 3 4 2 1 textStyle "bold" 1 0 "italic" 1 1 "f" 0 "g" 1 "b" 2 "c" 3 "stylesheet__world--thick" 4}}">World</span>!</h1>
93+
<div class={{-css-blocks-classnames 1 3 0 isWorld 1 2 1 0 "e" 0 "b" 1 "stylesheet__world--thick" 2}}>World</div>
94+
<div class={{-css-blocks-classnames 1 3 0 isWorld 1 0 1 2 "e" 0 "b" 1 "stylesheet__world--thick" 2}}>World</div>
95+
<div class={{-css-blocks-classnames 1 2 0 isWorld 0 1 1 "b" 0 "stylesheet__world--thick" 1}}>World</div>
9696
</div>
9797
`));
9898
});

0 commit comments

Comments
 (0)