1
1
import { h , Fragment } from 'preact' ;
2
- import { useEffect , useId } from 'preact/hooks' ;
2
+ import { useEffect , useId , useMemo } from 'preact/hooks' ;
3
3
import { SearchIcon , GlobeIcon } from '../../components/Icons.js' ;
4
4
import { useTypedTranslationWith } from '../../types' ;
5
5
import styles from './SearchForm.module.css' ;
@@ -8,6 +8,7 @@ import { useCompletionInput } from './useSuggestionInput.js';
8
8
import { useSuggestions } from './useSuggestions' ;
9
9
import { useSuffixText } from './SuffixText.js' ;
10
10
import { getInputSuffix } from '../utils.js' ;
11
+ import { usePlatformName } from '../../settings.provider.js' ;
11
12
12
13
/**
13
14
* @typedef {import('../strings.json') } Strings
@@ -26,6 +27,7 @@ import { getInputSuffix } from '../utils.js';
26
27
export function SearchForm ( { term, autoFocus, onChangeTerm, onOpenSuggestion, onSubmitSearch } ) {
27
28
const { t } = useTypedTranslationWith ( /** @type {Strings } */ ( { } ) ) ;
28
29
const suggestionsListId = useId ( ) ;
30
+ const platformName = usePlatformName ( ) ;
29
31
30
32
const {
31
33
suggestions,
@@ -45,10 +47,12 @@ export function SearchForm({ term, autoFocus, onChangeTerm, onOpenSuggestion, on
45
47
onSubmitSearch,
46
48
} ) ;
47
49
50
+ const inputRef = useCompletionInput ( inputBase , inputCompletion ) ;
51
+
48
52
const inputSuffix = getInputSuffix ( term , selectedSuggestion ) ;
49
53
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 ] ) ;
52
56
53
57
useEffect ( ( ) => {
54
58
if ( autoFocus && inputRef . current ) {
@@ -72,7 +76,7 @@ export function SearchForm({ term, autoFocus, onChangeTerm, onOpenSuggestion, on
72
76
onBlurCapture = { handleBlur }
73
77
onSubmit = { handleSubmit }
74
78
>
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` } } >
76
80
{ inputSuffix ?. kind === 'visit' ? < GlobeIcon inert /> : < SearchIcon inert /> }
77
81
< input
78
82
ref = { inputRef }
@@ -121,12 +125,14 @@ export function SearchForm({ term, autoFocus, onChangeTerm, onOpenSuggestion, on
121
125
122
126
/**
123
127
* @param {string } text
128
+ * @param {string } font
124
129
* @returns {number }
125
130
*/
126
- function measureText ( text ) {
131
+ function measureText ( text , font ) {
132
+ if ( ! text ) return 0 ;
127
133
const canvas = document . createElement ( 'canvas' ) ;
128
134
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 ;
131
137
return context . measureText ( text ) . width ;
132
138
}
0 commit comments