about summary refs log tree commit diff
path: root/src/components/Menu/context.tsx
blob: 908ad352eaa8290dba2e96ed9c5cbb9900a394e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import React from 'react'

import type {ContextType, ItemContextType} from '#/components/Menu/types'

export const Context = React.createContext<ContextType>({
  // @ts-ignore
  control: null,
})

export const ItemContext = React.createContext<ItemContextType>({
  disabled: false,
})

export function useMenuContext() {
  const context = React.useContext(Context)

  if (!context) {
    throw new Error('useMenuContext must be used within a Context.Provider')
  }

  return context
}

export function useMenuItemContext() {
  const context = React.useContext(ItemContext)

  if (!context) {
    throw new Error('useMenuItemContext must be used within a Context.Provider')
  }

  return context
}