Last active
December 10, 2021 12:00
-
-
Save yehuthi/89e2c0dae6155fc34db522c61bf0d6ae to your computer and use it in GitHub Desktop.
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
export default function (input: HTMLInputElement): { destroy: () => void } { | |
const ctx = document.createElement('canvas').getContext('2d')!; | |
ctx.font = window.getComputedStyle(input).font; | |
function handler() { | |
input.style.width = `${ctx.measureText(input.value).width}px`; | |
} | |
input.addEventListener('input', handler as any); | |
handler(); | |
return { | |
destroy: () => input.removeEventListener('input', handler as any), | |
}; | |
} |
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
const fontProps = [ | |
'fontFamily', | |
'fontSize', | |
'fontStretch', | |
'fontStyle', | |
'fontVariant', | |
'fontWeight', | |
] as const; | |
export default function (input: HTMLInputElement): { destroy: () => void } { | |
const proxy = document.createElement('span'); | |
proxy.style.position = 'fixed'; | |
proxy.style.opacity = '0'; | |
proxy.style.pointerEvents = 'none'; | |
input.parentNode!.appendChild(proxy); | |
function handler() { | |
const computedStyle = window.getComputedStyle(input); | |
// We don't just always set the font because Firefox doesn't compound all font data into it. | |
if (computedStyle.font) proxy.style.font = computedStyle.font; | |
else for (const prop of fontProps) proxy.style[prop] = computedStyle[prop]; | |
proxy.textContent = input.value; | |
input.style.width = `${proxy.clientWidth}px`; | |
} | |
input.addEventListener('input', handler as any); | |
setTimeout(handler); | |
return { | |
destroy: () => { | |
input.removeEventListener('input', handler as any); | |
proxy.parentNode!.removeChild(proxy); | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment