Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeps original classname as making classname pascal case would break the css. #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
lib
docs/.vitepress/cache
docs/.vitepress/dist
docs/.vitepress/dist
.DS_Store
.idea
6 changes: 3 additions & 3 deletions fixtures/Foo.mist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type FooProps = {

export function Foo({ children, fooSize, x, ...props }: FooProps) {
return (
<div {...props} className="Foo" data-fooSize={fooSize} data-x={x}>
<div {...props} className="foo" data-fooSize={fooSize} data-x={x}>
{children}
</div>
)
Expand All @@ -23,7 +23,7 @@ type BarProps = {

export function Bar({ children, barSize, x, ...props }: BarProps) {
return (
<span {...props} className="Bar" data-barSize={barSize} data-x={x}>
<span {...props} className="bar" data-barSize={barSize} data-x={x}>
{children}
</span>
)
Expand All @@ -35,7 +35,7 @@ type BazProps = {

export function Baz({ children, ...props }: BazProps) {
return (
<p {...props} className="Baz">
<p {...props} className="baz">
{children}
</p>
)
Expand Down
3 changes: 3 additions & 0 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ void test('parseInput', () => {
const actual: Components = parseInput(input)
const expected: Components = {
Foo: {
className: 'foo',
tag: 'div',
data: {
fooSize: ['lg', 'sm'],
x: true,
},
},
Bar: {
className: 'bar',
tag: 'span',
data: {
barSize: ['lg'],
x: true,
},
},
Baz: {
className: 'baz',
tag: 'p',
data: {},
},
Expand Down
8 changes: 5 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Components = Record<string, Component>
export type Component = {
tag: string
data: Record<string, string[] | boolean>
className?: string;
}

const enumDataAttributeRegex =
Expand Down Expand Up @@ -45,6 +46,7 @@ export function parseInput(input: string): Components {
const components: Components = {}

let name
let className
const nodes = visit(compile(input))
for (const node of nodes) {
// Parse name
Expand All @@ -53,9 +55,9 @@ export function parseInput(input: string): Components {
if (prop === undefined) {
throw new Error('Invalid MistCSS file, no class found in @scope')
}
name = prop.replace('(.', '').replace(')', '')
name = pascalCase(name)
components[name] = { tag: '', data: {} }
className = prop.replace('(.', '').replace(')', '')
name = pascalCase(className)
components[name] = { tag: '', data: {}, className }
continue
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function ${name}({ ${[
<${[
component.tag,
'{...props}',
`className="${name}"`,
`className="${component?.className ?? name}"`,
...Object.keys(component.data).map((key) => `data-${key}={${key}}`),
].join(' ')}>
{children}
Expand Down
Loading