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

fix(svelte): merge-props style #2047

Merged
merged 2 commits into from
Nov 24, 2024
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
5 changes: 5 additions & 0 deletions .changeset/light-tomatoes-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/svelte": patch
---

Fix style merging in svelte `mergeProps`
2 changes: 1 addition & 1 deletion packages/frameworks/svelte/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { mergeProps } from "@zag-js/core"
export { mergeProps } from "./merge-props"
export type { ContextFrom, EventFrom, StateFrom } from "@zag-js/core"
export { normalizeProps } from "./normalize-props.js"
export type { PropTypes } from "./normalize-props.js"
Expand Down
34 changes: 34 additions & 0 deletions packages/frameworks/svelte/src/merge-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { mergeProps as zagMergeProps } from "@zag-js/core"

const CSS_REGEX = /((?:--)?(?:\w+-?)+)\s*:\s*([^;]*)/g

type CSSObject = Record<string, string>

const serialize = (style: string): CSSObject => {
const res: Record<string, string> = {}
let match: RegExpExecArray | null
while ((match = CSS_REGEX.exec(style))) {
res[match[1]!] = match[2]!
}
return res
}

const css = (style: CSSObject | string | undefined): string => {
if (typeof style === "string") style = serialize(style)

const mergedString = Object.entries(style as CSSObject)
.map(([key, value]) => `${key}: ${value}`)
.join("; ")

return mergedString
}

export function mergeProps(...args: Record<string, any>[]) {
const merged = zagMergeProps(...args)

if ("style" in merged) {
merged.style = css(merged.style)
}

return merged
}
Loading