Skip to content

Commit

Permalink
fix(svelte): merge-props style (#2047)
Browse files Browse the repository at this point in the history
* fix(svelte): merge-props style

* chore: add changeset
  • Loading branch information
anubra266 authored Nov 24, 2024
1 parent faec46d commit 44106f6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
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
}

0 comments on commit 44106f6

Please sign in to comment.