Skip to content

Commit 31819c7

Browse files
The-Code-MonkeyAndy Wilson
andauthored
fix: keeps original classname as making classname pascal case would break the css. (#29)
* fix: update so original clss name comes through from css * fix: tests now pass --------- Co-authored-by: Andy Wilson <andy@builtbypixel.com>
1 parent fb029fe commit 31819c7

5 files changed

Lines changed: 15 additions & 8 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
22
lib
33
docs/.vitepress/cache
4-
docs/.vitepress/dist
4+
docs/.vitepress/dist
5+
.DS_Store
6+
.idea

fixtures/Foo.mist.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type FooProps = {
99

1010
export function Foo({ children, fooSize, x, ...props }: FooProps) {
1111
return (
12-
<div {...props} className="Foo" data-fooSize={fooSize} data-x={x}>
12+
<div {...props} className="foo" data-fooSize={fooSize} data-x={x}>
1313
{children}
1414
</div>
1515
)
@@ -23,7 +23,7 @@ type BarProps = {
2323

2424
export function Bar({ children, barSize, x, ...props }: BarProps) {
2525
return (
26-
<span {...props} className="Bar" data-barSize={barSize} data-x={x}>
26+
<span {...props} className="bar" data-barSize={barSize} data-x={x}>
2727
{children}
2828
</span>
2929
)
@@ -35,7 +35,7 @@ type BazProps = {
3535

3636
export function Baz({ children, ...props }: BazProps) {
3737
return (
38-
<p {...props} className="Baz">
38+
<p {...props} className="baz">
3939
{children}
4040
</p>
4141
)

src/parser.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@ void test('parseInput', () => {
2626
const actual: Components = parseInput(input)
2727
const expected: Components = {
2828
Foo: {
29+
className: 'foo',
2930
tag: 'div',
3031
data: {
3132
fooSize: ['lg', 'sm'],
3233
x: true,
3334
},
3435
},
3536
Bar: {
37+
className: 'bar',
3638
tag: 'span',
3739
data: {
3840
barSize: ['lg'],
3941
x: true,
4042
},
4143
},
4244
Baz: {
45+
className: 'baz',
4346
tag: 'p',
4447
data: {},
4548
},

src/parser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type Components = Record<string, Component>
66
export type Component = {
77
tag: string
88
data: Record<string, string[] | boolean>
9+
className?: string;
910
}
1011

1112
const enumDataAttributeRegex =
@@ -45,6 +46,7 @@ export function parseInput(input: string): Components {
4546
const components: Components = {}
4647

4748
let name
49+
let className
4850
const nodes = visit(compile(input))
4951
for (const node of nodes) {
5052
// Parse name
@@ -53,9 +55,9 @@ export function parseInput(input: string): Components {
5355
if (prop === undefined) {
5456
throw new Error('Invalid MistCSS file, no class found in @scope')
5557
}
56-
name = prop.replace('(.', '').replace(')', '')
57-
name = pascalCase(name)
58-
components[name] = { tag: '', data: {} }
58+
className = prop.replace('(.', '').replace(')', '')
59+
name = pascalCase(className)
60+
components[name] = { tag: '', data: {}, className }
5961
continue
6062
}
6163

src/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function ${name}({ ${[
3838
<${[
3939
component.tag,
4040
'{...props}',
41-
`className="${name}"`,
41+
`className="${component?.className ?? name}"`,
4242
...Object.keys(component.data).map((key) => `data-${key}={${key}}`),
4343
].join(' ')}>
4444
{children}

0 commit comments

Comments
 (0)