-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from tmg0/fix/scale
fix: children element scale error
- Loading branch information
Showing
4 changed files
with
78 additions
and
8 deletions.
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
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,49 @@ | ||
import { tryOnMounted, tryOnUnmounted } from '@vueuse/core' | ||
import { type MaybeRef, ref, unref } from 'vue' | ||
|
||
function parseTransform(value: string) { | ||
const translate3dMatch = /translate3d\(([^)]+)\)/.exec(value) | ||
const scaleXMatch = /scaleX\(([\d.]+)\)/.exec(value) | ||
const scaleYMatch = /scaleY\(([\d.]+)\)/.exec(value) | ||
|
||
const [x, y, z] = translate3dMatch | ||
? translate3dMatch[1].split(',').map(val => Number.parseFloat(val)) | ||
: [0, 0, 0] | ||
|
||
return { | ||
translateX: x, | ||
translateY: y, | ||
translateZ: z, | ||
scaleX: scaleXMatch ? Number.parseFloat(scaleXMatch[1]) : 1, | ||
scaleY: scaleYMatch ? Number.parseFloat(scaleYMatch[1]) : 1, | ||
} | ||
} | ||
|
||
export function useStyle(target: MaybeRef<HTMLElement | SVGElement | undefined>) { | ||
let observer: MutationObserver | ||
|
||
const transform = ref<Record<string, number>>({}) | ||
|
||
tryOnMounted(() => { | ||
observer = new MutationObserver((mutations) => { | ||
mutations.forEach(({ target }) => { | ||
transform.value = parseTransform((target as any).style.transform ?? '') | ||
}) | ||
}) | ||
|
||
const elt = unref(target) | ||
|
||
if (!elt) | ||
return | ||
|
||
observer.observe(elt, { attributes: true, attributeFilter: ['style'] }) | ||
}) | ||
|
||
tryOnUnmounted(() => { | ||
observer?.disconnect() | ||
}) | ||
|
||
return { | ||
transform, | ||
} | ||
} |
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