Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 9x 9x 3x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 9x 18x 18x 9x 9x 9x 1x | import {rule, useTheme} from 'nano-theme';
import * as React from 'react';
import {copyStyles} from './util';
const blockClass = rule({
d: 'inline-block',
pos: 'relative',
});
const inputClass = rule({
d: 'inline-block',
va: 'bottom',
bxz: 'border-box',
ov: 'hidden',
pd: 0,
mr: 0,
bd: 0,
bg: 0,
out: 0,
col: 'inherit',
fw: 'inherit',
f: 'inherit',
lh: 'inherit',
ws: 'pre',
resize: 'none',
});
const sizerClass = rule({
d: 'inline-block',
pos: 'absolute',
pe: 'none',
us: 'none',
bxz: 'border-box',
bd: 0,
t: 0,
l: 0,
ws: 'pre',
});
export interface FlexibleInputProps {
/** Ref to the input element. */
inp?: (el: HTMLInputElement | HTMLTextAreaElement | null) => void;
/** Value to display, when used as a controlled component. */
value?: string;
/**
* Initial value to display, when used as an uncontrolled component (used
* together with {@link FlexibleInputProps.uncontrolled}).
*/
defaultValue?: string;
/**
* Whether the value is *not* controlled by React. When `true`, the `value`
* prop is ignored and the underlying element manages its own value — this is
* the mode to use when the value is driven externally, for example by a
* `<CollaborativeInput>` CRDT binding. Use the imperative {@link
* FlexibleInputHandle.resize} method to re-measure after such external
* changes.
*/
uncontrolled?: boolean;
/** Whether the input is multiline. */
multiline?: boolean;
/** Whether to wrap text to a new line when it exceeds the available width. */
wrap?: boolean;
/**
* Whether the input should take the full width of the parent, even when there
* is not enough text to do that naturally with content.
*/
fullWidth?: boolean;
/** Placeholder to display. */
typebefore?: string;
/** Typeahead string to add to the value. It is visible at half opacity. */
typeahead?: string;
/** Addition width to add, for example, to account for number stepper. */
extraWidth?: number;
/** Minimum width to allow. */
minWidth?: number;
/** Maximum width to allow. */
maxWidth?: number;
/** Whether the input is focused on initial render. */
focus?: boolean;
/** Callback for when the value changes. */
onChange?: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>;
/** Callback for when the input is focused. */
onFocus?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;
/** Callback for when the input is blurred. */
onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;
/** Callback for when a key is pressed. */
onKeyDown?: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
/** Callback for when the Enter key is pressed. */
onSubmit?: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
/** Callback for when the Escape key is pressed. */
onCancel?: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
/** Callback for when the Tab key is pressed. */
onTab?: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement>;
}
/** Imperative API exposed through a `ref` to {@link FlexibleInput}. */
export interface FlexibleInputHandle {
/** The underlying `<input>` or `<textarea>` element. */
readonly input: HTMLInputElement | HTMLTextAreaElement | null;
/**
* Re-measure the element against its current value and resize it. Call this
* after the value has been changed externally (i.e. not through user input),
* for example when the value is driven by a CRDT binding.
*/
resize: () => void;
/** Focus the input element. */
focus: () => void;
}
export const FlexibleInput = React.forwardRef<FlexibleInputHandle, FlexibleInputProps>((props, ref) => {
const {
inp,
value,
defaultValue,
uncontrolled,
multiline,
wrap,
fullWidth,
typebefore = '',
typeahead = '',
extraWidth,
minWidth = 8,
maxWidth,
focus,
onChange,
onFocus,
onBlur,
onKeyDown,
onSubmit,
onCancel,
onTab,
} = props;
const inputRef = React.useRef<HTMLInputElement | HTMLTextAreaElement | null>(null);
const sizerRef = React.useRef<HTMLSpanElement>(null);
const sizerValueRef = React.useRef<HTMLSpanElement>(null);
const theme = useTheme();
const resize = React.useCallback(() => {
const input = inputRef.current;
const sizer = sizerRef.current;
Iif (!input || !sizer) return;
// Mirror the element's *actual* value into the sizer, so that resizing
// works regardless of whether the value is controlled by React or driven
// externally.
const sizerValue = sizerValueRef.current;
Eif (sizerValue) sizerValue.textContent = input.value;
const style = input.style;
Iif (fullWidth) {
style.width = '100%';
} else {
let width = sizer.scrollWidth;
Iif (extraWidth) width += extraWidth;
Eif (minWidth) width = Math.max(width, minWidth);
Iif (maxWidth) width = Math.min(width, maxWidth);
style.width = width + 'px';
}
if (multiline) style.height = sizer.scrollHeight + 'px';
}, [fullWidth, extraWidth, minWidth, maxWidth, multiline]);
const focusFn = React.useCallback(() => inputRef.current?.focus(), []);
React.useImperativeHandle(
ref,
() => ({
get input() {
return inputRef.current;
},
resize,
focus: focusFn,
}),
[resize, focusFn],
);
// biome-ignore lint/correctness/useExhaustiveDependencies: run once on mount
React.useLayoutEffect(() => {
const input = inputRef.current;
const sizer = sizerRef.current;
Iif (!input || !sizer) return;
Iif (focus) input.focus();
copyStyles(input, sizer, [
'font',
'fontSize',
'fontFamily',
'fontWeight',
'fontStyle',
'letterSpacing',
'textTransform',
'boxSizing',
]);
}, []);
// biome-ignore lint/correctness/useExhaustiveDependencies: re-measure when the rendered text/layout changes
React.useLayoutEffect(() => {
resize();
}, [resize, value, typeahead, wrap]);
const handleChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement> = (e) => {
resize();
Eif (onChange) onChange(e);
};
const attr: React.InputHTMLAttributes<HTMLInputElement | HTMLTextAreaElement> & {ref: React.RefCallback<any>} = {
ref: (el) => {
inputRef.current = el;
if (inp) inp(el);
},
className: inputClass,
style: {
width: fullWidth ? '100%' : undefined,
display: fullWidth ? 'block' : 'inline-block',
whiteSpace: wrap ? 'pre-wrap' : 'pre',
},
onChange: handleChange,
onFocus,
onBlur,
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
if (e.key === 'Enter' && (!multiline || e.ctrlKey)) {
if (onSubmit) onSubmit(e);
} else if (e.key === 'Escape') {
if (onCancel) onCancel(e);
} else if (e.key === 'Tab') {
if (onTab) onTab(e);
}
if (onKeyDown) onKeyDown(e);
},
};
const elementValue = uncontrolled ? undefined : value;
const input = multiline ? (
<textarea {...attr} value={elementValue} defaultValue={uncontrolled ? defaultValue : undefined} />
) : (
<input {...attr} value={elementValue} defaultValue={uncontrolled ? defaultValue : undefined} />
);
return (
<>
{!!typebefore && !fullWidth && <span style={{color: theme.g(0.7), verticalAlign: 'top'}}>{typebefore}</span>}
<span className={blockClass} style={fullWidth ? {display: 'block', width: '100%', overflowX: 'auto'} : undefined}>
{input}
<span
ref={sizerRef}
className={sizerClass}
style={{width: fullWidth ? '100%' : undefined, whiteSpace: wrap ? 'pre-wrap' : 'pre'}}
>
{/* Content is filled imperatively in `resize()` from the live element value. */}
<span ref={sizerValueRef} style={{visibility: 'hidden'}} />
{'\u200b'}
{!!typeahead && <span style={{color: theme.g(0.7)}}>{typeahead}</span>}
</span>
</span>
</>
);
});
FlexibleInput.displayName = 'FlexibleInput';
|