-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use one observer. Get value from
--smooth-corners
- Loading branch information
1 parent
a863063
commit 6fe6d02
Showing
4 changed files
with
64 additions
and
54 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
This file was deleted.
Oops, something went wrong.
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,38 @@ | ||
import type { MutableRefObject } from "react"; | ||
import { useEffect } from "react"; | ||
import { getSvgPath } from "figma-squircle"; | ||
|
||
function setCorners(entry: ResizeObserverEntry) { | ||
const element = entry.target as HTMLElement; | ||
const styles = window.getComputedStyle(element); | ||
const { width, height, top, right, bottom, left } = entry.contentRect; | ||
|
||
const clipPath = getSvgPath({ | ||
width: left + width + right, | ||
height: top + height + bottom, | ||
cornerRadius: parseInt(styles.borderRadius, 10), | ||
cornerSmoothing: Number(styles.getPropertyValue('--smooth-corners')) | ||
}); | ||
|
||
element.style.clipPath = `path('${clipPath}')`; | ||
} | ||
|
||
let smoothCornersObserver = null; | ||
|
||
if ('ResizeObserver' in globalThis) { | ||
smoothCornersObserver = new ResizeObserver((entries) => { | ||
for (const entry of entries) { | ||
setCorners(entry); | ||
} | ||
}); | ||
} | ||
|
||
export function useSmoothCorners(cardRef: MutableRefObject<HTMLElement>) { | ||
useEffect(() => { | ||
smoothCornersObserver.observe(cardRef.current); | ||
|
||
return () => { | ||
smoothCornersObserver.unobserve(cardRef.current); | ||
}; | ||
}, []); | ||
} |
6fe6d02
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.
Deploy preview for transport ready!
✅ Preview
https://transport-6mboqc4km-ekbdev.vercel.app
https://ekbdev-transport-feature-card-smooth-corners.vercel.app
Built with commit 6fe6d02.
This pull request is being automatically deployed with vercel-action