Skip to content

Commit 621556e

Browse files
authored
NTP: Fix input styling on Windows (#1862)
* Add Windows-specific text selection styling * Reduce input font weight to 400 on Windows
1 parent 33c5321 commit 621556e

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

special-pages/pages/new-tab/app/omnibar/components/AiChatForm.module.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111
border: none;
1212
box-sizing: content-box;
1313
color: var(--ntp-text-normal);
14-
font-weight: 500;
1514
max-height: 10lh;
1615
padding: 11px 15px 0;
1716
resize: none;
1817

18+
[data-platform="macos"] & {
19+
font-weight: 500;
20+
}
21+
1922
&::placeholder {
2023
color: var(--ntp-text-muted);
2124
}
2225

26+
[data-platform="windows"] &::selection {
27+
background: var(--ntp-selection-background-color);
28+
color: var(--ntp-selection-color);
29+
}
30+
2331
&:focus {
2432
outline: none;
2533
}

special-pages/pages/new-tab/app/omnibar/components/SearchForm.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h, Fragment } from 'preact';
2-
import { useEffect, useId } from 'preact/hooks';
2+
import { useEffect, useId, useMemo } from 'preact/hooks';
33
import { SearchIcon, GlobeIcon } from '../../components/Icons.js';
44
import { useTypedTranslationWith } from '../../types';
55
import styles from './SearchForm.module.css';
@@ -8,6 +8,7 @@ import { useCompletionInput } from './useSuggestionInput.js';
88
import { useSuggestions } from './useSuggestions';
99
import { useSuffixText } from './SuffixText.js';
1010
import { 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';
2627
export 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
}

special-pages/pages/new-tab/app/omnibar/components/SearchForm.module.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
background: none;
1414
border: none;
1515
color: var(--ntp-text-normal);
16-
font-weight: 500;
16+
font: var(--input-font);
1717
height: var(--sp-8);
1818
left: 7px;
1919
padding-bottom: 0;
@@ -27,13 +27,18 @@
2727
color: var(--ntp-text-muted);
2828
}
2929

30+
[data-platform="windows"] &::selection {
31+
background: var(--ntp-selection-background-color);
32+
color: var(--ntp-selection-color);
33+
}
34+
3035
&:focus {
3136
outline: none;
3237
}
3338
}
3439

3540
.suffixSpacer {
36-
font-weight: 500;
41+
font: var(--input-font);
3742
overflow: hidden;
3843
visibility: hidden;
3944
white-space: nowrap;
@@ -42,4 +47,5 @@
4247
.suffix {
4348
color: var(--ntp-color-primary);
4449
flex: none;
50+
font: var(--input-font);
4551
}

special-pages/pages/new-tab/app/styles/ntp-theme.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ body {
5858
--ntp-surface-tertiary: var(--color-white);
5959
--ntp-controls-raised-backdrop: var(--color-black-at-9);
6060
--ntp-controls-raised-fill-primary: var(--color-white);
61+
--ntp-selection-color: var(--color-white);
62+
--ntp-selection-background-color: #3A69EF;
6163
}
6264

6365
[data-theme=dark] {
@@ -75,6 +77,8 @@ body {
7577
--ntp-surface-tertiary: rgba(71, 71, 71, 0.66);
7678
--ntp-controls-raised-backdrop: var(--color-black-at-60);
7779
--ntp-controls-raised-fill-primary: var(--color-white-at-18);
80+
--ntp-selection-color: var(--color-white);
81+
--ntp-selection-background-color: var(--color-blue-30);
7882
}
7983

8084
/* This comes from the application settings */

0 commit comments

Comments
 (0)