Skip to content

Commit

Permalink
fix: useId backward compatibility (#165)
Browse files Browse the repository at this point in the history
* only use useId hook from react when available

* prefix id to avoid external conflict

* prefer @radix-ui react id

---------

Co-authored-by: paco <[email protected]>
  • Loading branch information
phsantiago and pacocoursey authored Mar 14, 2024
1 parent 4e01e2b commit 72e2137
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"@radix-ui/react-dialog": "1.0.5",
"@radix-ui/react-id": "^1.0.1",
"@radix-ui/react-primitive": "1.0.3"
},
"devDependencies": {
Expand Down
23 changes: 17 additions & 6 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as RadixDialog from '@radix-ui/react-dialog'
import * as React from 'react'
import { commandScore } from './command-score'
import { Primitive } from '@radix-ui/react-primitive'
import { useId } from '@radix-ui/react-id'

type Children = { children?: React.ReactNode }
type DivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>
Expand Down Expand Up @@ -163,6 +164,16 @@ const useStore = () => React.useContext(StoreContext)
// @ts-ignore
const GroupContext = React.createContext<Group>(undefined)

const getId = (() => {
let i = 0
return () => `${i++}`
})()
const useIdCompatibility = () => {
React.useState(getId)
const [id] = React.useState(getId)
return 'cmdk' + id
}

const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwardedRef) => {
const state = useLazyRef<State>(() => ({
/** Value of the search query. */
Expand Down Expand Up @@ -196,9 +207,9 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
...etc
} = props

const listId = React.useId()
const labelId = React.useId()
const inputId = React.useId()
const listId = useId()
const labelId = useId()
const inputId = useId()

const listInnerRef = React.useRef<HTMLDivElement>(null)

Expand Down Expand Up @@ -641,7 +652,7 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
* the rendered item's `textContent`.
*/
const Item = React.forwardRef<HTMLDivElement, ItemProps>((props, forwardedRef) => {
const id = React.useId()
const id = useId()
const ref = React.useRef<HTMLDivElement>(null)
const groupContext = React.useContext(GroupContext)
const context = useCommand()
Expand Down Expand Up @@ -707,10 +718,10 @@ const Item = React.forwardRef<HTMLDivElement, ItemProps>((props, forwardedRef) =
*/
const Group = React.forwardRef<HTMLDivElement, GroupProps>((props, forwardedRef) => {
const { heading, children, forceMount, ...etc } = props
const id = React.useId()
const id = useId()
const ref = React.useRef<HTMLDivElement>(null)
const headingRef = React.useRef<HTMLDivElement>(null)
const headingId = React.useId()
const headingId = useId()
const context = useCommand()
const render = useCmdk((state) =>
forceMount ? true : context.filter() === false ? true : !state.search ? true : state.filtered.groups.has(id),
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 72e2137

Please sign in to comment.