Skip to content

Commit a6d83c3

Browse files
committed
Update docs
1 parent 1e4510b commit a6d83c3

16 files changed

+1351
-24
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ dist
99
compiled
1010
.awcache
1111
.rpt2_cache
12-
docs
12+

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2017 Ahmad Al Haddad <haddad@ah.sa>
1+
Copyright 2020 Ahmad Al Haddad <haddad@ah.sa>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ React.render(<App />, rootElement);
127127

128128
## To do
129129

130+
- Improve documentation.
130131
- Use popper for positioning menus.
131132

132133
## Examples

docs/assets/css/main.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/images/icons.png

9.39 KB
Loading

docs/assets/images/icons@2x.png

27.5 KB
Loading

docs/assets/images/widgets.png

480 Bytes
Loading

docs/assets/images/widgets@2x.png

855 Bytes
Loading

docs/assets/js/main.js

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/js/search.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"kinds":{"64":"Function","256":"Interface","1024":"Property","4194304":"Type alias"},"rows":[{"id":0,"kind":256,"name":"MenuItem","url":"interfaces/menuitem.html","classes":"tsd-kind-interface"},{"id":1,"kind":1024,"name":"id","url":"interfaces/menuitem.html#id","classes":"tsd-kind-property tsd-parent-kind-interface","parent":"MenuItem"},{"id":2,"kind":1024,"name":"label","url":"interfaces/menuitem.html#label","classes":"tsd-kind-property tsd-parent-kind-interface","parent":"MenuItem"},{"id":3,"kind":1024,"name":"subMenu","url":"interfaces/menuitem.html#submenu","classes":"tsd-kind-property tsd-parent-kind-interface","parent":"MenuItem"},{"id":4,"kind":256,"name":"NestedMenuProps","url":"interfaces/nestedmenuprops.html","classes":"tsd-kind-interface"},{"id":5,"kind":1024,"name":"items","url":"interfaces/nestedmenuprops.html#items","classes":"tsd-kind-property tsd-parent-kind-interface","parent":"NestedMenuProps"},{"id":6,"kind":1024,"name":"isOpen","url":"interfaces/nestedmenuprops.html#isopen","classes":"tsd-kind-property tsd-parent-kind-interface","parent":"NestedMenuProps"},{"id":7,"kind":1024,"name":"defaultOpenPath","url":"interfaces/nestedmenuprops.html#defaultopenpath","classes":"tsd-kind-property tsd-parent-kind-interface","parent":"NestedMenuProps"},{"id":8,"kind":4194304,"name":"Items","url":"globals.html#items","classes":"tsd-kind-type-alias"},{"id":9,"kind":4194304,"name":"Action","url":"globals.html#action","classes":"tsd-kind-type-alias"},{"id":10,"kind":64,"name":"useNestedMenu","url":"globals.html#usenestedmenu","classes":"tsd-kind-function"}],"index":{"version":"2.3.9","fields":["name","parent"],"fieldVectors":[["name/0",[0,9.808]],["parent/0",[]],["name/1",[1,20.794]],["parent/1",[0,0.731]],["name/2",[2,20.794]],["parent/2",[0,0.731]],["name/3",[3,20.794]],["parent/3",[0,0.731]],["name/4",[4,9.808]],["parent/4",[]],["name/5",[5,15.686]],["parent/5",[4,0.731]],["name/6",[6,20.794]],["parent/6",[4,0.731]],["name/7",[7,20.794]],["parent/7",[4,0.731]],["name/8",[5,15.686]],["parent/8",[]],["name/9",[8,20.794]],["parent/9",[]],["name/10",[9,20.794]],["parent/10",[]]],"invertedIndex":[["action",{"_index":8,"name":{"9":{}},"parent":{}}],["defaultopenpath",{"_index":7,"name":{"7":{}},"parent":{}}],["id",{"_index":1,"name":{"1":{}},"parent":{}}],["isopen",{"_index":6,"name":{"6":{}},"parent":{}}],["items",{"_index":5,"name":{"5":{},"8":{}},"parent":{}}],["label",{"_index":2,"name":{"2":{}},"parent":{}}],["menuitem",{"_index":0,"name":{"0":{}},"parent":{"1":{},"2":{},"3":{}}}],["nestedmenuprops",{"_index":4,"name":{"4":{}},"parent":{"5":{},"6":{},"7":{}}}],["submenu",{"_index":3,"name":{"3":{}},"parent":{}}],["usenestedmenu",{"_index":9,"name":{"10":{}},"parent":{}}]],"pipeline":[]}}

docs/globals.html

Lines changed: 416 additions & 0 deletions
Large diffs are not rendered by default.

docs/index.html

Lines changed: 417 additions & 0 deletions
Large diffs are not rendered by default.

docs/interfaces/menuitem.html

Lines changed: 203 additions & 0 deletions
Large diffs are not rendered by default.

docs/interfaces/nestedmenuprops.html

Lines changed: 210 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
{
22
"name": "react-headless-nested-menu",
3-
"version": "0.0.0-development",
4-
"description": "",
5-
"keywords": [],
3+
"version": "0.5.0",
4+
"description": "A useful headless component (hook) that gives you all the functions you need to create a multi-level menu using your own components!",
5+
"keywords": [
6+
"menu",
7+
"nested menu",
8+
"multi-level menu",
9+
"multilevel menu",
10+
"mega menu",
11+
"headless ui",
12+
"headless component",
13+
"hook"
14+
],
615
"main": "dist/react-headless-nested-menu.umd.js",
716
"module": "dist/react-headless-nested-menu.es5.js",
817
"typings": "dist/types/react-headless-nested-menu.d.ts",
@@ -21,7 +30,7 @@
2130
"scripts": {
2231
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
2332
"prebuild": "rimraf dist",
24-
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src",
33+
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src --exclude 'src/utils.ts'",
2534
"start": "rollup -c rollup.config.ts -w",
2635
"test": "jest --coverage",
2736
"test:watch": "jest --coverage --watch",

src/react-headless-nested-menu.tsx

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
// Import here Polyfills if needed. Recommended core-js (npm i -D core-js)
2-
// ...
3-
41
import React from 'react'
52
import produce, { Draft } from 'immer'
63
import getEventPath, { handleRefs, getDirection } from './utils'
74

8-
const { useRef, useReducer, useEffect, useCallback } = React
9-
105
interface MenuItem {
116
id: string
127
label: string
@@ -15,28 +10,43 @@ interface MenuItem {
1510

1611
export type Items = MenuItem[]
1712

13+
/**
14+
* @ignore
15+
*/
1816
interface ToggleAction {
1917
type: 'toggle'
2018
}
2119

20+
/**
21+
* @ignore
22+
*/
2223
interface OpenPathAction {
2324
type: 'open-path'
2425
id: string
2526
}
2627

28+
/**
29+
* @ignore
30+
*/
2731
interface ClosePathAction {
2832
type: 'close-path'
2933
id: string
3034
}
3135

3236
type Action = ToggleAction | OpenPathAction | ClosePathAction
3337

38+
/**
39+
* @ignore
40+
*/
3441
interface NestedMenuState {
3542
items: Items
3643
isOpen: boolean
3744
currentPath: string[]
3845
}
3946

47+
/**
48+
* @ignore
49+
*/
4050
const reducer = produce((draft: Draft<NestedMenuState>, action: Action) => {
4151
switch (action.type) {
4252
case 'toggle':
@@ -56,29 +66,38 @@ const reducer = produce((draft: Draft<NestedMenuState>, action: Action) => {
5666
}
5767
})
5868

69+
/**
70+
* The props you pass to `useNestedMenu(props) as args`
71+
*
72+
* @category Types
73+
*/
5974
interface NestedMenuProps {
6075
items?: Items
6176
isOpen?: boolean
6277
defaultOpenPath?: string[]
6378
}
6479

65-
interface HitAreaProps {
66-
anchor?: DOMRect
67-
menu?: DOMRect
68-
}
80+
// interface HitAreaProps {
81+
// anchor?: DOMRect
82+
// menu?: DOMRect
83+
// }
6984

85+
/**
86+
*
87+
* @category Hooks
88+
*/
7089
export const useNestedMenu = ({
7190
items = [],
7291
isOpen = false,
7392
defaultOpenPath = [],
7493
}: NestedMenuProps) => {
75-
const [state, dispatch] = useReducer(reducer, {
94+
const [state, dispatch] = React.useReducer(reducer, {
7695
items,
7796
isOpen,
7897
currentPath: defaultOpenPath,
7998
})
8099

81-
const globalClickHandler = useCallback(
100+
const globalClickHandler = React.useCallback(
82101
(event: MouseEvent) => {
83102
const path = getEventPath(event)
84103
const items = Object.keys(itemRefs.current).map((id) => itemRefs.current[id]) as EventTarget[]
@@ -96,7 +115,7 @@ export const useNestedMenu = ({
96115
[state.isOpen]
97116
)
98117

99-
useEffect(() => {
118+
React.useEffect(() => {
100119
if (state.isOpen) {
101120
document.addEventListener('click', globalClickHandler)
102121
}
@@ -131,7 +150,7 @@ export const useNestedMenu = ({
131150

132151
const isSubMenuOpen = (item: MenuItem) => state.currentPath.indexOf(item.id) !== -1
133152

134-
const toggleButtonRef = useRef(null!)
153+
const toggleButtonRef = React.useRef(null!)
135154

136155
const getToggleButtonProps = () => ({
137156
onClick() {
@@ -140,7 +159,7 @@ export const useNestedMenu = ({
140159
ref: toggleButtonRef,
141160
})
142161

143-
const menuRefs = useRef<{ [key: string]: HTMLElement }>({})
162+
const menuRefs = React.useRef<{ [key: string]: HTMLElement }>({})
144163

145164
const getMenuProps = (item?: MenuItem) => ({
146165
key: item?.id || 'root',
@@ -151,7 +170,7 @@ export const useNestedMenu = ({
151170
}),
152171
})
153172

154-
const itemRefs = useRef<{ [key: string]: HTMLElement }>({})
173+
const itemRefs = React.useRef<{ [key: string]: HTMLElement }>({})
155174

156175
const getItemProps = (item: MenuItem) => ({
157176
key: item.id,
@@ -224,8 +243,8 @@ export const useNestedMenu = ({
224243
const getItemPath = (item: MenuItem) => [...state.currentPath, item.id]
225244

226245
// still not working properly
227-
const anchorRef = useRef<HTMLElement>()
228-
const menuRef = useRef<HTMLElement>()
246+
const anchorRef = React.useRef<HTMLElement>()
247+
const menuRef = React.useRef<HTMLElement>()
229248

230249
return {
231250
getToggleButtonProps,
@@ -236,7 +255,6 @@ export const useNestedMenu = ({
236255
getCloseTriggerProps,
237256
getToggleTriggerProps,
238257
getItemPath,
239-
items: state.items,
240258
isOpen: state.isOpen,
241259
currentPath: state.currentPath,
242260
openPath,

0 commit comments

Comments
 (0)