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

Handle custom fonts loading #362

Merged
merged 6 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/tricky-jokes-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-textarea-autosize': patch
---

Support custom fonts loading
Andarist marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
}
</style>
<title>React &lt;TextareaAutosize /&gt; component</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Work+Sans:wght@800&display=swap"
rel="stylesheet"
/>
</head>
<body>
<h1>React &lt;TextareaAutosize /&gt; component</h1>
Expand Down
28 changes: 24 additions & 4 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const ControlledMode = () => {
<TextareaAutosize
cacheMeasurements
value={value}
onChange={ev => setValue(ev.target.value)}
onChange={(ev) => setValue(ev.target.value)}
/>
<button onClick={() => setValue('This value was set programatically')}>
{'Change value programatically'}
Expand Down Expand Up @@ -156,7 +156,7 @@ const OnHeightChangeCallback = () => {
</pre>
<TextareaAutosize
cacheMeasurements
onHeightChange={height => {
onHeightChange={(height) => {
// eslint-disable-next-line no-console
console.log(height);
}}
Expand All @@ -173,16 +173,35 @@ const MultipleTextareas = () => {
<div>{'This one controls the rest.'}</div>
<TextareaAutosize
value={value}
onChange={ev => setValue(ev.target.value)}
onChange={(ev) => setValue(ev.target.value)}
/>
<div>{'Those get controlled by the one above.'}</div>
{range(15).map(i => (
{range(15).map((i) => (
<TextareaAutosize key={i} value={value} />
))}
</div>
);
};

const WithCustomFont = () => {
return (
<div>
<h2>{'Adapts to custom fonts.'}</h2>
<div>{'Resizes once the font is loaded.'}</div>
<TextareaAutosize
style={{
fontSize: 20,
fontFamily: "'Work Sans', sans-serif",
}}
defaultValue={'The quick brown fox jumps over the lazy dog'}
onHeightChange={(rows) => {
console.log('onChange', rows);
}}
/>
</div>
);
};

const Demo = () => {
return (
<div>
Expand All @@ -195,6 +214,7 @@ const Demo = () => {
<UncontrolledMode />
<OnHeightChangeCallback />
<MultipleTextareas />
<WithCustomFont />
</div>
);
};
Expand Down
17 changes: 17 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ export const useWindowResizeListener = (listener: (event: UIEvent) => any) => {
};
}, []);
};

export const useFontsLoadedListener = (listener: () => any) => {
ArnaudRinquin marked this conversation as resolved.
Show resolved Hide resolved
const latestListener = useLatest(listener);

React.useLayoutEffect(() => {
const handler: typeof listener = () => {
console.log('loaded!');
ArnaudRinquin marked this conversation as resolved.
Show resolved Hide resolved
latestListener.current();
};

document.fonts.addEventListener('loadingdone', handler);

return () => {
document.fonts.removeEventListener('loadingdone', handler);
};
}, []);
};
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as React from 'react';
import calculateNodeHeight from './calculateNodeHeight';
import getSizingData, { SizingData } from './getSizingData';
import { useComposedRef, useWindowResizeListener } from './hooks';
import {
useComposedRef,
useWindowResizeListener,
useFontsLoadedListener,
} from './hooks';
import { noop } from './utils';

type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>;
Expand Down Expand Up @@ -93,6 +97,7 @@ const TextareaAutosize: React.ForwardRefRenderFunction<
if (typeof document !== 'undefined') {
React.useLayoutEffect(resizeTextarea);
useWindowResizeListener(resizeTextarea);
useFontsLoadedListener(resizeTextarea);
ArnaudRinquin marked this conversation as resolved.
Show resolved Hide resolved
}

return <textarea {...props} onChange={handleChange} ref={ref} />;
Expand Down