Skip to content

Commit

Permalink
Refactor root ref into global context
Browse files Browse the repository at this point in the history
  • Loading branch information
i-like-robots committed Feb 14, 2024
1 parent d0798c9 commit 3998dbf
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/components/ReactTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function ReactTags(
const comboBoxRef = useRef<HTMLDivElement>(null)
const inputRef = useRef<HTMLInputElement>(null)
const listBoxRef = useRef<HTMLDivElement>(null)
const rootRef = useRef<HTMLDivElement>(null)

const managerRef = useManager({
activateFirstOption,
Expand Down Expand Up @@ -194,6 +195,7 @@ function ReactTags(
isInvalid,
listBoxRef,
managerRef,
rootRef,
}}
>
<Root onBlur={onBlur} onFocus={onFocus} render={renderRoot}>
Expand Down
1 change: 1 addition & 0 deletions src/contexts/GlobalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type GlobalContextValue = {
isInvalid: boolean
listBoxRef: React.MutableRefObject<HTMLDivElement | null>
managerRef: React.MutableRefObject<UseManagerState>
rootRef: React.MutableRefObject<HTMLDivElement | null>
}

export const GlobalContext = React.createContext<GlobalContextValue | undefined>(undefined)
5 changes: 2 additions & 3 deletions src/hooks/useRoot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useMemo, useRef, useState } from 'react'
import React, { useContext, useMemo, useState } from 'react'
import { GlobalContext } from '../contexts'
import { labelId, rootId } from '../lib'
import type { OnBlur, OnFocus } from '../sharedTypes'
Expand All @@ -15,8 +15,7 @@ export type UseRootState = {

export function useRoot({ onBlur, onFocus }: UseRootArgs): UseRootState {
const [isActive, setIsActive] = useState<boolean>(false)
const { id, inputRef } = useContext(GlobalContext)
const rootRef = useRef<HTMLDivElement>(null)
const { id, inputRef, rootRef } = useContext(GlobalContext)

const rootProps = useMemo(() => {
return {
Expand Down

0 comments on commit 3998dbf

Please sign in to comment.