-
Notifications
You must be signed in to change notification settings - Fork 249
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
Fix handling of dynamic className in tailwind input #849
Merged
Kitenite
merged 7 commits into
onlook-dev:main
from
cleobnvntra:bugfix/dynamic-css-classnames
Dec 6, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
700f90b
Fix handling of dynamic className in tailwind input
cleobnvntra 21df662
Fix handling of dynamic className in tailwind input
cleobnvntra e5ef2e4
Merge branch 'bugfix/dynamic-css-classnames' of https://github.com/cl…
cleobnvntra 8ed0826
Merge branch 'main' into bugfix/dynamic-css-classnames
cleobnvntra 36efeca
reformat logic for handling template literals
cleobnvntra 8479ab3
Separate error from classNames
Kitenite c303ffe
Move model for shared
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,75 @@ | ||
import * as t from '@babel/types'; | ||
import type { ClassParsingResult, TemplateNode } from '@onlook/models/element'; | ||
import { readCodeBlock } from '.'; | ||
import { parseJsxCodeBlock } from './helpers'; | ||
import type { TemplateNode } from '@onlook/models/element'; | ||
|
||
export async function getTemplateNodeClass(templateNode: TemplateNode): Promise<string[]> { | ||
export async function getTemplateNodeClass( | ||
templateNode: TemplateNode, | ||
): Promise<ClassParsingResult> { | ||
const codeBlock = await readCodeBlock(templateNode); | ||
const ast = parseJsxCodeBlock(codeBlock); | ||
|
||
if (!ast) { | ||
return []; | ||
return { type: 'error', reason: 'AST could not be parsed.' }; | ||
} | ||
|
||
const classes = getNodeClasses(ast); | ||
return classes || []; | ||
return getNodeClasses(ast); | ||
} | ||
|
||
function getNodeClasses(node: t.JSXElement): string[] { | ||
function getNodeClasses(node: t.JSXElement): ClassParsingResult { | ||
const openingElement = node.openingElement; | ||
const classNameAttr = openingElement.attributes.find( | ||
(attr): attr is t.JSXAttribute => t.isJSXAttribute(attr) && attr.name.name === 'className', | ||
); | ||
|
||
if (!classNameAttr) { | ||
return []; | ||
return { | ||
type: 'error', | ||
reason: 'No className attribute found.', | ||
}; | ||
} | ||
|
||
if (t.isStringLiteral(classNameAttr.value)) { | ||
return classNameAttr.value.value.split(/\s+/).filter(Boolean); | ||
return { | ||
type: 'classes', | ||
value: classNameAttr.value.value.split(/\s+/).filter(Boolean), | ||
}; | ||
} | ||
|
||
if ( | ||
t.isJSXExpressionContainer(classNameAttr.value) && | ||
t.isStringLiteral(classNameAttr.value.expression) | ||
) { | ||
return classNameAttr.value.expression.value.split(/\s+/).filter(Boolean); | ||
return { | ||
type: 'classes', | ||
value: classNameAttr.value.expression.value.split(/\s+/).filter(Boolean), | ||
}; | ||
} | ||
|
||
if ( | ||
t.isJSXExpressionContainer(classNameAttr.value) && | ||
t.isTemplateLiteral(classNameAttr.value.expression) | ||
) { | ||
const templateLiteral = classNameAttr.value.expression; | ||
|
||
// Immediately return error if dynamic classes are detected within the template literal | ||
if (templateLiteral.expressions.length > 0) { | ||
return { | ||
type: 'error', | ||
reason: 'Dynamic classes detected.', | ||
}; | ||
} | ||
|
||
// Extract and return static classes from the template literal if no dynamic classes are used | ||
const quasis = templateLiteral.quasis.map((quasi) => quasi.value.raw.split(/\s+/)); | ||
return { | ||
type: 'classes', | ||
value: quasis.flat().filter(Boolean), | ||
}; | ||
} | ||
|
||
return []; | ||
return { | ||
type: 'error', | ||
reason: 'Unsupported className format.', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
interface ParsedClasses { | ||
type: 'classes'; | ||
value: string[]; | ||
} | ||
|
||
interface ClassParsingError { | ||
type: 'error'; | ||
reason: string; | ||
} | ||
|
||
export type ClassParsingResult = ParsedClasses | ClassParsingError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { z } from 'zod'; | ||
|
||
const BaseDomElementSchema = z.object({ | ||
domId: z.string(), | ||
webviewId: z.string(), | ||
oid: z.string().nullable(), | ||
instanceId: z.string().nullable(), | ||
rect: z.instanceof(DOMRect), | ||
}); | ||
|
||
export const ParentDomElementSchema = BaseDomElementSchema; | ||
|
||
export const DomElementSchema = BaseDomElementSchema.extend({ | ||
tagName: z.string(), | ||
styles: z.record(z.string(), z.string()), | ||
parent: ParentDomElementSchema.nullable(), | ||
}); | ||
|
||
export const ElementPositionSchema = z.object({ | ||
x: z.number(), | ||
y: z.number(), | ||
}); | ||
|
||
export const DropElementPropertiesSchema = z.object({ | ||
tagName: z.string(), | ||
styles: z.record(z.string(), z.string()), | ||
textContent: z.string().nullable(), | ||
}); | ||
|
||
export type DomElement = z.infer<typeof DomElementSchema>; | ||
export type ParentDomElement = z.infer<typeof ParentDomElementSchema>; | ||
export type ElementPosition = z.infer<typeof ElementPositionSchema>; | ||
export type DropElementProperties = z.infer<typeof DropElementPropertiesSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,4 @@ | ||
import { z } from 'zod'; | ||
|
||
const BaseDomElementSchema = z.object({ | ||
domId: z.string(), | ||
webviewId: z.string(), | ||
oid: z.string().nullable(), | ||
instanceId: z.string().nullable(), | ||
rect: z.instanceof(DOMRect), | ||
}); | ||
|
||
export const ParentDomElementSchema = BaseDomElementSchema; | ||
|
||
export const DomElementSchema = BaseDomElementSchema.extend({ | ||
tagName: z.string(), | ||
styles: z.record(z.string(), z.string()), | ||
parent: ParentDomElementSchema.nullable(), | ||
}); | ||
|
||
export const ElementPositionSchema = z.object({ | ||
x: z.number(), | ||
y: z.number(), | ||
}); | ||
|
||
export const DropElementPropertiesSchema = z.object({ | ||
tagName: z.string(), | ||
styles: z.record(z.string(), z.string()), | ||
textContent: z.string().nullable(), | ||
}); | ||
|
||
export type DomElement = z.infer<typeof DomElementSchema>; | ||
export type ParentDomElement = z.infer<typeof ParentDomElementSchema>; | ||
export type ElementPosition = z.infer<typeof ElementPositionSchema>; | ||
export type DropElementProperties = z.infer<typeof DropElementPropertiesSchema>; | ||
|
||
export * from './classes'; | ||
export * from './element'; | ||
export * from './layers'; | ||
export * from './templateNode'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also return an error straight from here since we're only handling classes. Everything else passes through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reformatted the code into:
This allows the code to exit the block early if it is an error (dynamic classes are used).
I am unsure if this is exactly what you meant. Can you please clarify further if I misinterpreted it? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works great, thanks!