11import { h , Fragment } from 'preact' ;
2- import { useEffect , useId } from 'preact/hooks' ;
2+ import { useEffect , useId , useMemo } from 'preact/hooks' ;
33import { SearchIcon , GlobeIcon } from '../../components/Icons.js' ;
44import { useTypedTranslationWith } from '../../types' ;
55import styles from './SearchForm.module.css' ;
@@ -8,6 +8,7 @@ import { useCompletionInput } from './useSuggestionInput.js';
88import { useSuggestions } from './useSuggestions' ;
99import { useSuffixText } from './SuffixText.js' ;
1010import { getInputSuffix } from '../utils.js' ;
11+ import { usePlatformName } from '../../settings.provider.js' ;
1112
1213/**
1314 * @typedef {import('../strings.json') } Strings
@@ -26,6 +27,7 @@ import { getInputSuffix } from '../utils.js';
2627export function SearchForm ( { term, autoFocus, onChangeTerm, onOpenSuggestion, onSubmitSearch } ) {
2728 const { t } = useTypedTranslationWith ( /** @type {Strings } */ ( { } ) ) ;
2829 const suggestionsListId = useId ( ) ;
30+ const platformName = usePlatformName ( ) ;
2931
3032 const {
3133 suggestions,
@@ -45,10 +47,12 @@ export function SearchForm({ term, autoFocus, onChangeTerm, onOpenSuggestion, on
4547 onSubmitSearch,
4648 } ) ;
4749
50+ const inputRef = useCompletionInput ( inputBase , inputCompletion ) ;
51+
4852 const inputSuffix = getInputSuffix ( term , selectedSuggestion ) ;
4953 const inputSuffixText = useSuffixText ( inputSuffix ) ;
50-
51- const inputRef = useCompletionInput ( inputBase , inputCompletion ) ;
54+ const inputFont = platformName === 'windows' ? '400 13px / 16px system-ui' : '500 13px / 16px system-ui' ;
55+ const inputSuffixWidth = useMemo ( ( ) => measureText ( inputSuffixText , inputFont ) , [ inputSuffixText , inputFont ] ) ;
5256
5357 useEffect ( ( ) => {
5458 if ( autoFocus && inputRef . current ) {
@@ -72,7 +76,7 @@ export function SearchForm({ term, autoFocus, onChangeTerm, onOpenSuggestion, on
7276 onBlurCapture = { handleBlur }
7377 onSubmit = { handleSubmit }
7478 >
75- < div class = { styles . inputContainer } style = { { '--suffix-text-width' : `${ measureText ( inputSuffixText ) } px` } } >
79+ < div class = { styles . inputContainer } style = { { '--input-font' : inputFont , '-- suffix-text-width' : `${ inputSuffixWidth } px` } } >
7680 { inputSuffix ?. kind === 'visit' ? < GlobeIcon inert /> : < SearchIcon inert /> }
7781 < input
7882 ref = { inputRef }
@@ -121,12 +125,14 @@ export function SearchForm({ term, autoFocus, onChangeTerm, onOpenSuggestion, on
121125
122126/**
123127 * @param {string } text
128+ * @param {string } font
124129 * @returns {number }
125130 */
126- function measureText ( text ) {
131+ function measureText ( text , font ) {
132+ if ( ! text ) return 0 ;
127133 const canvas = document . createElement ( 'canvas' ) ;
128134 const context = canvas . getContext ( '2d' ) ;
129- if ( ! context ) return 0 ;
130- context . font = '500 13px / 16px system-ui' ;
135+ if ( ! context ) throw new Error ( 'Failed to get canvas context' ) ;
136+ context . font = font ;
131137 return context . measureText ( text ) . width ;
132138}
0 commit comments