Skip to content

Commit

Permalink
feat: use react compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 1, 2024
1 parent 289a005 commit 6aeeb37
Show file tree
Hide file tree
Showing 29 changed files with 384 additions and 426 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = {
'react/prop-types': 'off',
'react-hooks/exhaustive-deps': 'error', // Checks effect dependencies
'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
'react-compiler/react-compiler': 'warn', // Set to error once existing warnings are fixed
'react-compiler/react-compiler': 'error',
'react/no-unescaped-entities': 'off',
'no-restricted-imports': [
'error',
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- run: pnpm install
- name: Register Problem Matcher for ESLint that handles -f compact and shows warnings and errors inline on PRs
run: echo "::add-matcher::.github/eslint-compact.json"
- run: "pnpm lint -f compact --rule 'no-warning-comments: [off]' --max-warnings 12"
- run: "pnpm lint -f compact --rule 'no-warning-comments: [off]' --max-warnings 0"

test:
needs: [build]
Expand Down
4 changes: 3 additions & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const config: StorybookConfig = {
return mergeConfig(config, {
plugins: [
viteReact({
babel: {plugins: [['babel-plugin-react-compiler', {target: '18'}]]},
babel: {
plugins: [['babel-plugin-react-compiler', {target: '18'}]],
},
}),
tsconfigPaths(),
],
Expand Down
39 changes: 30 additions & 9 deletions package.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
import {defineConfig} from '@sanity/pkg-utils'
import { defineConfig } from "@sanity/pkg-utils";

Check failure on line 1 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `·defineConfig·}·from·"@sanity/pkg-utils";` with `defineConfig}·from·'@sanity/pkg-utils'`

export default defineConfig({
extract: {
rules: {
'ae-internal-missing-underscore': 'off',
'ae-incompatible-release-tags': 'warn',
'ae-missing-release-tag': 'warn',
"ae-internal-missing-underscore": "off",

Check failure on line 6 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"ae-internal-missing-underscore":·"off"` with `'ae-internal-missing-underscore':·'off'`
"ae-incompatible-release-tags": "warn",

Check failure on line 7 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"ae-incompatible-release-tags":·"warn"` with `'ae-incompatible-release-tags':·'warn'`
"ae-missing-release-tag": "warn",

Check failure on line 8 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"ae-missing-release-tag":·"warn"` with `'ae-missing-release-tag':·'warn'`
},
},
legacyExports: true,
strictOptions: {
// disable warning when not using browserslist in package.json
noImplicitBrowsersList: 'off',
noImplicitBrowsersList: "off",

Check failure on line 14 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"off"` with `'off'`
},
tsconfig: 'tsconfig.dist.json',
babel: {reactCompiler: true},
reactCompilerOptions: {target: '18'},
})
tsconfig: "tsconfig.dist.json",

Check failure on line 16 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"tsconfig.dist.json"` with `'tsconfig.dist.json'`
babel: { reactCompiler: true },

Check failure on line 17 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `·reactCompiler:·true·` with `reactCompiler:·true`
reactCompilerOptions: {
target: "18",

Check failure on line 19 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"18"` with `'18'`
logger: {
logEvent(filename, event) {
/* eslint-disable no-console */
if (event.kind === "CompileError") {

Check failure on line 23 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Replace `"CompileError"` with `'CompileError'`
console.group(`[${filename}] ${event.kind}`);

Check failure on line 24 in package.config.ts

View workflow job for this annotation

GitHub Actions / ESLint

Delete `;`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { reason, description, severity, loc, suggestions } =
event.detail as any;

console.error(`[${severity}] ${reason}`);
console.log(
`${filename}:${loc.start?.line}:${loc.start?.column} ${description}`,
);
console.log(suggestions);

console.groupEnd();
}
},
},
},
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sanity/ui",
"version": "2.8.17",
"version": "2.9.0-canary.6",
"keywords": [
"sanity",
"ui",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 42 additions & 31 deletions src/core/components/autocomplete/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useMemo,
useReducer,
useRef,
useState,
} from 'react'
import {EMPTY_ARRAY, EMPTY_RECORD} from '../../constants'
import {_hasFocus, _raf, focusFirstDescendant} from '../../helpers'
Expand Down Expand Up @@ -184,21 +185,22 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
typeof filterOptionProp === 'function' ? filterOptionProp : DEFAULT_FILTER_OPTION

// Element refs
const rootElementRef = useRef<HTMLDivElement | null>(null)
const resultsPopoverElementRef = useRef<HTMLDivElement | null>(null)
const inputElementRef = useRef<HTMLInputElement | null>(null)
const [rootElement, setRootElement] = useState<HTMLDivElement | null>(null)
const [resultsPopoverElement, setResultsPopoverElement] = useState<HTMLDivElement | null>(null)
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null)
const listBoxElementRef = useRef<HTMLDivElement | null>(null)

// Value refs
const listFocusedRef = useRef(false)
const valueRef = useRef(value)
const valuePropRef = useRef(valueProp)
const popoverMouseWithinRef = useRef(false)
const [popoverMouseWithin, setPopoverMouseWithin] = useState(false)

// Forward ref to parent
useImperativeHandle<HTMLInputElement | null, HTMLInputElement | null>(
forwardedRef,
() => inputElementRef.current,
() => inputElement,
[inputElement],
)

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

const elements: HTMLElement[] = (relatedElements || []).concat(
rootElementRef.current ? [rootElementRef.current] : [],
resultsPopoverElementRef.current ? [resultsPopoverElementRef.current] : [],
rootElement ? [rootElement] : [],
resultsPopoverElement ? [resultsPopoverElement] : [],
)

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

if (focusInside === false) {
dispatch({type: 'root/blur'})
popoverMouseWithinRef.current = false
setPopoverMouseWithin(false)
if (onQueryChange) onQueryChange(null)
if (onBlur) onBlur(event)
}
}, 0)
},
[onBlur, onQueryChange, relatedElements],
[
onBlur,
onQueryChange,
popoverMouseWithin,
relatedElements,
resultsPopoverElement,
rootElement,
],
)

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

popoverMouseWithinRef.current = false
setPopoverMouseWithin(false)

if (onSelect) onSelect(v)

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

inputElementRef.current?.focus()
inputElement?.focus()
},
[onChange, onSelect, onQueryChange],
[onSelect, onChange, onQueryChange, inputElement],
)

const handleRootKeyDown = useCallback(
Expand Down Expand Up @@ -324,9 +333,9 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<

if (event.key === 'Escape') {
dispatch({type: 'root/escape'})
popoverMouseWithinRef.current = false
setPopoverMouseWithin(false)
if (onQueryChange) onQueryChange(null)
inputElementRef.current?.focus()
inputElement?.focus()

return
}
Expand All @@ -338,12 +347,12 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
(listEl === target || listEl?.contains(target)) &&
!AUTOCOMPLETE_LISTBOX_IGNORE_KEYS.includes(event.key)
) {
inputElementRef.current?.focus()
inputElement?.focus()

return
}
},
[activeValue, filteredOptions, filteredOptionsLen, onQueryChange],
[activeValue, filteredOptions, filteredOptionsLen, inputElement, onQueryChange],
)

const handleInputChange = useCallback(
Expand Down Expand Up @@ -377,20 +386,20 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
)

const handlePopoverMouseEnter = useCallback(() => {
popoverMouseWithinRef.current = true
setPopoverMouseWithin(true)
}, [])

const handlePopoverMouseLeave = useCallback(() => {
popoverMouseWithinRef.current = false
setPopoverMouseWithin(false)
}, [])

const handleClearButtonClick = useCallback(() => {
dispatch({type: 'root/clear'})
valueRef.current = ''
if (onChange) onChange('')
if (onQueryChange) onQueryChange(null)
inputElementRef.current?.focus()
}, [onChange, onQueryChange])
inputElement?.focus()
}, [inputElement, onChange, onQueryChange])

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

if (openButtonProps.onClick) openButtonProps.onClick(event)

_raf(() => inputElementRef.current?.focus())
_raf(() => inputElement?.focus())
},
[openButtonProps, dispatchOpen],
[dispatchOpen, openButtonProps, inputElement],
)

const openButtonNode = useMemo(
Expand Down Expand Up @@ -554,7 +563,7 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
prefix={prefix}
radius={radius}
readOnly={readOnly}
ref={inputElementRef}
ref={setInputElement}
role="combobox"
spellCheck={false}
suffix={suffix || openButtonNode}
Expand All @@ -566,10 +575,10 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
(event: KeyboardEvent<HTMLDivElement>) => {
// If the focus is currently in the list, move focus to the input element
if (event.key === 'Tab') {
if (listFocused) inputElementRef.current?.focus()
if (listFocused) inputElement?.focus()
}
},
[listFocused],
[inputElement, listFocused],
)

const content = useMemo(() => {
Expand Down Expand Up @@ -635,11 +644,11 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
{
content,
hidden: !expanded,
inputElement: inputElementRef.current,
inputElement,
onMouseEnter: handlePopoverMouseEnter,
onMouseLeave: handlePopoverMouseLeave,
},
resultsPopoverElementRef,
{current: resultsPopoverElement},
)
}

Expand All @@ -661,8 +670,8 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
placement={AUTOCOMPLETE_POPOVER_PLACEMENT}
portal
radius={radius}
ref={resultsPopoverElementRef}
referenceElement={inputElementRef.current}
ref={setResultsPopoverElement}
referenceElement={inputElement}
{...popover}
/>
)
Expand All @@ -672,9 +681,11 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
filteredOptionsLen,
handlePopoverMouseEnter,
handlePopoverMouseLeave,
inputElement,
popover,
radius,
renderPopover,
resultsPopoverElement,
])

return (
Expand All @@ -683,7 +694,7 @@ const InnerAutocomplete = forwardRef(function InnerAutocomplete<
onBlur={handleRootBlur}
onFocus={handleRootFocus}
onKeyDown={handleRootKeyDown}
ref={rootElementRef}
ref={setRootElement}
>
{input}
{results}
Expand Down
Loading

0 comments on commit 6aeeb37

Please sign in to comment.