Skip to content

Commit 6aeeb37

Browse files
committed
feat: use react compiler
1 parent 289a005 commit 6aeeb37

29 files changed

+384
-426
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = {
9090
'react/prop-types': 'off',
9191
'react-hooks/exhaustive-deps': 'error', // Checks effect dependencies
9292
'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
93-
'react-compiler/react-compiler': 'warn', // Set to error once existing warnings are fixed
93+
'react-compiler/react-compiler': 'error',
9494
'react/no-unescaped-entities': 'off',
9595
'no-restricted-imports': [
9696
'error',

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- run: pnpm install
3939
- name: Register Problem Matcher for ESLint that handles -f compact and shows warnings and errors inline on PRs
4040
run: echo "::add-matcher::.github/eslint-compact.json"
41-
- run: "pnpm lint -f compact --rule 'no-warning-comments: [off]' --max-warnings 12"
41+
- run: "pnpm lint -f compact --rule 'no-warning-comments: [off]' --max-warnings 0"
4242

4343
test:
4444
needs: [build]

.storybook/main.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const config: StorybookConfig = {
2525
return mergeConfig(config, {
2626
plugins: [
2727
viteReact({
28-
babel: {plugins: [['babel-plugin-react-compiler', {target: '18'}]]},
28+
babel: {
29+
plugins: [['babel-plugin-react-compiler', {target: '18'}]],
30+
},
2931
}),
3032
tsconfigPaths(),
3133
],

package.config.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
import {defineConfig} from '@sanity/pkg-utils'
1+
import { defineConfig } from "@sanity/pkg-utils";
22

33
export default defineConfig({
44
extract: {
55
rules: {
6-
'ae-internal-missing-underscore': 'off',
7-
'ae-incompatible-release-tags': 'warn',
8-
'ae-missing-release-tag': 'warn',
6+
"ae-internal-missing-underscore": "off",
7+
"ae-incompatible-release-tags": "warn",
8+
"ae-missing-release-tag": "warn",
99
},
1010
},
1111
legacyExports: true,
1212
strictOptions: {
1313
// disable warning when not using browserslist in package.json
14-
noImplicitBrowsersList: 'off',
14+
noImplicitBrowsersList: "off",
1515
},
16-
tsconfig: 'tsconfig.dist.json',
17-
babel: {reactCompiler: true},
18-
reactCompilerOptions: {target: '18'},
19-
})
16+
tsconfig: "tsconfig.dist.json",
17+
babel: { reactCompiler: true },
18+
reactCompilerOptions: {
19+
target: "18",
20+
logger: {
21+
logEvent(filename, event) {
22+
/* eslint-disable no-console */
23+
if (event.kind === "CompileError") {
24+
console.group(`[${filename}] ${event.kind}`);
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
const { reason, description, severity, loc, suggestions } =
27+
event.detail as any;
28+
29+
console.error(`[${severity}] ${reason}`);
30+
console.log(
31+
`${filename}:${loc.start?.line}:${loc.start?.column} ${description}`,
32+
);
33+
console.log(suggestions);
34+
35+
console.groupEnd();
36+
}
37+
},
38+
},
39+
},
40+
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sanity/ui",
3-
"version": "2.8.17",
3+
"version": "2.9.0-canary.6",
44
"keywords": [
55
"sanity",
66
"ui",

pnpm-lock.yaml

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/components/autocomplete/autocomplete.tsx

+42-31
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
useMemo,
1818
useReducer,
1919
useRef,
20+
useState,
2021
} from 'react'
2122
import {EMPTY_ARRAY, EMPTY_RECORD} from '../../constants'
2223
import {_hasFocus, _raf, focusFirstDescendant} from '../../helpers'
@@ -184,21 +185,22 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
184185
typeof filterOptionProp === 'function' ? filterOptionProp : DEFAULT_FILTER_OPTION
185186

186187
// Element refs
187-
const rootElementRef = useRef<HTMLDivElement | null>(null)
188-
const resultsPopoverElementRef = useRef<HTMLDivElement | null>(null)
189-
const inputElementRef = useRef<HTMLInputElement | null>(null)
188+
const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null)
189+
const [resultsPopoverElement, setResultsPopoverElement] = useState<HTMLDivElement | null>(null)
190+
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null)
190191
const listBoxElementRef = useRef<HTMLDivElement | null>(null)
191192

192193
// Value refs
193194
const listFocusedRef = useRef(false)
194195
const valueRef = useRef(value)
195196
const valuePropRef = useRef(valueProp)
196-
const popoverMouseWithinRef = useRef(false)
197+
const [popoverMouseWithin, setPopoverMouseWithin] = useState(false)
197198

198199
// Forward ref to parent
199200
useImperativeHandle<HTMLInputElement | null, HTMLInputElement | null>(
200201
forwardedRef,
201-
() => inputElementRef.current,
202+
() => inputElement,
203+
[inputElement],
202204
)
203205

204206
const listBoxId = `${id}-listbox`
@@ -222,13 +224,13 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
222224
// NOTE: This is a workaround for a bug that may happen in Chrome (clicking the scrollbar
223225
// closes the results in certain situations):
224226
// - Do not handle blur if the mouse is within the popover
225-
if (popoverMouseWithinRef.current) {
227+
if (popoverMouseWithin) {
226228
return
227229
}
228230

229231
const elements: HTMLElement[] = (relatedElements || []).concat(
230-
rootElementRef.current ? [rootElementRef.current] : [],
231-
resultsPopoverElementRef.current ? [resultsPopoverElementRef.current] : [],
232+
rootElement ? [rootElement] : [],
233+
resultsPopoverElement ? [resultsPopoverElement] : [],
232234
)
233235

234236
let focusInside = false
@@ -244,13 +246,20 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
244246

245247
if (focusInside === false) {
246248
dispatch({type: 'root/blur'})
247-
popoverMouseWithinRef.current = false
249+
setPopoverMouseWithin(false)
248250
if (onQueryChange) onQueryChange(null)
249251
if (onBlur) onBlur(event)
250252
}
251253
}, 0)
252254
},
253-
[onBlur, onQueryChange, relatedElements],
255+
[
256+
onBlur,
257+
onQueryChange,
258+
popoverMouseWithin,
259+
relatedElements,
260+
resultsPopoverElement,
261+
rootElement,
262+
],
254263
)
255264

256265
const handleRootFocus = useCallback((event: FocusEvent<HTMLDivElement>) => {
@@ -269,7 +278,7 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
269278
(v: string) => {
270279
dispatch({type: 'value/change', value: v})
271280

272-
popoverMouseWithinRef.current = false
281+
setPopoverMouseWithin(false)
273282

274283
if (onSelect) onSelect(v)
275284

@@ -278,9 +287,9 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
278287
if (onChange) onChange(v)
279288
if (onQueryChange) onQueryChange(null)
280289

281-
inputElementRef.current?.focus()
290+
inputElement?.focus()
282291
},
283-
[onChange, onSelect, onQueryChange],
292+
[onSelect, onChange, onQueryChange, inputElement],
284293
)
285294

286295
const handleRootKeyDown = useCallback(
@@ -324,9 +333,9 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
324333

325334
if (event.key === 'Escape') {
326335
dispatch({type: 'root/escape'})
327-
popoverMouseWithinRef.current = false
336+
setPopoverMouseWithin(false)
328337
if (onQueryChange) onQueryChange(null)
329-
inputElementRef.current?.focus()
338+
inputElement?.focus()
330339

331340
return
332341
}
@@ -338,12 +347,12 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
338347
(listEl === target || listEl?.contains(target)) &&
339348
!AUTOCOMPLETE_LISTBOX_IGNORE_KEYS.includes(event.key)
340349
) {
341-
inputElementRef.current?.focus()
350+
inputElement?.focus()
342351

343352
return
344353
}
345354
},
346-
[activeValue, filteredOptions, filteredOptionsLen, onQueryChange],
355+
[activeValue, filteredOptions, filteredOptionsLen, inputElement, onQueryChange],
347356
)
348357

349358
const handleInputChange = useCallback(
@@ -377,20 +386,20 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
377386
)
378387

379388
const handlePopoverMouseEnter = useCallback(() => {
380-
popoverMouseWithinRef.current = true
389+
setPopoverMouseWithin(true)
381390
}, [])
382391

383392
const handlePopoverMouseLeave = useCallback(() => {
384-
popoverMouseWithinRef.current = false
393+
setPopoverMouseWithin(false)
385394
}, [])
386395

387396
const handleClearButtonClick = useCallback(() => {
388397
dispatch({type: 'root/clear'})
389398
valueRef.current = ''
390399
if (onChange) onChange('')
391400
if (onQueryChange) onQueryChange(null)
392-
inputElementRef.current?.focus()
393-
}, [onChange, onQueryChange])
401+
inputElement?.focus()
402+
}, [inputElement, onChange, onQueryChange])
394403

395404
const handleClearButtonFocus = useCallback(() => {
396405
dispatch({type: 'input/focus'})
@@ -482,9 +491,9 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
482491

483492
if (openButtonProps.onClick) openButtonProps.onClick(event)
484493

485-
_raf(() => inputElementRef.current?.focus())
494+
_raf(() => inputElement?.focus())
486495
},
487-
[openButtonProps, dispatchOpen],
496+
[dispatchOpen, openButtonProps, inputElement],
488497
)
489498

490499
const openButtonNode = useMemo(
@@ -554,7 +563,7 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
554563
prefix={prefix}
555564
radius={radius}
556565
readOnly={readOnly}
557-
ref={inputElementRef}
566+
ref={setInputElement}
558567
role="combobox"
559568
spellCheck={false}
560569
suffix={suffix || openButtonNode}
@@ -566,10 +575,10 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
566575
(event: KeyboardEvent<HTMLDivElement>) => {
567576
// If the focus is currently in the list, move focus to the input element
568577
if (event.key === 'Tab') {
569-
if (listFocused) inputElementRef.current?.focus()
578+
if (listFocused) inputElement?.focus()
570579
}
571580
},
572-
[listFocused],
581+
[inputElement, listFocused],
573582
)
574583

575584
const content = useMemo(() => {
@@ -635,11 +644,11 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
635644
{
636645
content,
637646
hidden: !expanded,
638-
inputElement: inputElementRef.current,
647+
inputElement,
639648
onMouseEnter: handlePopoverMouseEnter,
640649
onMouseLeave: handlePopoverMouseLeave,
641650
},
642-
resultsPopoverElementRef,
651+
{current: resultsPopoverElement},
643652
)
644653
}
645654

@@ -661,8 +670,8 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
661670
placement={AUTOCOMPLETE_POPOVER_PLACEMENT}
662671
portal
663672
radius={radius}
664-
ref={resultsPopoverElementRef}
665-
referenceElement={inputElementRef.current}
673+
ref={setResultsPopoverElement}
674+
referenceElement={inputElement}
666675
{...popover}
667676
/>
668677
)
@@ -672,9 +681,11 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
672681
filteredOptionsLen,
673682
handlePopoverMouseEnter,
674683
handlePopoverMouseLeave,
684+
inputElement,
675685
popover,
676686
radius,
677687
renderPopover,
688+
resultsPopoverElement,
678689
])
679690

680691
return (
@@ -683,7 +694,7 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
683694
onBlur={handleRootBlur}
684695
onFocus={handleRootFocus}
685696
onKeyDown={handleRootKeyDown}
686-
ref={rootElementRef}
697+
ref={setRootElement}
687698
>
688699
{input}
689700
{results}

0 commit comments

Comments
 (0)