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(signature): add dark mode support for signature drawing #47

Merged
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
47 changes: 29 additions & 18 deletions components/ui/signature-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,36 @@ export default function SignatureInput({

useEffect(() => {
const canvas = canvasRef.current
if (canvas) {
if (!canvas) return

const updateStrokeColor = () => {
const ctx = canvas.getContext('2d')
if (ctx) {
const width = 440
const height = 220
// scaling the canvas for better image quality but
canvas.width = width * SCALE
canvas.height = height * SCALE
// keeping display size of the canvas the same
canvas.style.width = `${width}px`
canvas.style.height = `${height}px`
ctx.scale(SCALE, SCALE)

ctx.lineWidth = 2
ctx.lineCap = 'round'
ctx.strokeStyle = 'black'
}
// removing scroll behavior on touch screens, while drawing
return disableTouchScroll(canvas)
if (!ctx) return

const isDarkClass = document.documentElement.classList.contains('dark')
const isLightClass = document.documentElement.classList.contains('light')

const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches

const isDarkMode = isDarkClass || (!isLightClass && systemPrefersDark)

ctx.strokeStyle = isDarkMode ? '#ffffff' : '#000000'
}

updateStrokeColor()

const observer = new MutationObserver(updateStrokeColor)
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class']
})

const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
mediaQuery.addEventListener('change', updateStrokeColor)

return () => {
observer.disconnect()
mediaQuery.removeEventListener('change', updateStrokeColor)
}
}, [canvasRef])

Expand Down