@cypress-design/react-docmenu 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,5 +2,5 @@ $ yarn build:module && yarn build:types
2
2
  $ rollup -c ./rollup.config.mjs
3
3
  
4
4
  ./index.ts → ./dist/index.umd.js, ./dist/index.es.mjs...
5
- created ./dist/index.umd.js, ./dist/index.es.mjs in 33.9s
5
+ created ./dist/index.umd.js, ./dist/index.es.mjs in 13.3s
6
6
  $ tsc --project ./tsconfig.build.json
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @cypress-design/react-docmenu
2
2
 
3
+ ## 0.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#353](https://github.com/cypress-io/cypress-design/pull/353) [`b382b9e`](https://github.com/cypress-io/cypress-design/commit/b382b9ecc99e3ab6288eee406831199edb661c19) Thanks [@elevatebart](https://github.com/elevatebart)! - feat: Docgen wrapping active links
8
+
9
+ - Updated dependencies [[`b382b9e`](https://github.com/cypress-io/cypress-design/commit/b382b9ecc99e3ab6288eee406831199edb661c19)]:
10
+ - @cypress-design/constants-docmenu@0.1.2
11
+
12
+ ## 0.4.0
13
+
14
+ ### Minor Changes
15
+
16
+ - [#350](https://github.com/cypress-io/cypress-design/pull/350) [`abc414d`](https://github.com/cypress-io/cypress-design/commit/abc414da149eb6fe0cfa61497a0aa8630d8d8d6e) Thanks [@elevatebart](https://github.com/elevatebart)! - feat: allow usage of a custom link component
17
+
18
+ ### Patch Changes
19
+
20
+ - [#347](https://github.com/cypress-io/cypress-design/pull/347) [`de67952`](https://github.com/cypress-io/cypress-design/commit/de679522ea2269854ec8ac8de925fcf930ce96e4) Thanks [@elevatebart](https://github.com/elevatebart)! - fix: docmenu docusaurus style items structure
21
+
22
+ - Updated dependencies [[`de67952`](https://github.com/cypress-io/cypress-design/commit/de679522ea2269854ec8ac8de925fcf930ce96e4)]:
23
+ - @cypress-design/constants-docmenu@0.1.1
24
+
3
25
  ## 0.3.1
4
26
 
5
27
  ### Patch Changes
package/DocMenu.tsx CHANGED
@@ -1,33 +1,63 @@
1
1
  import * as React from 'react'
2
2
  import { NavGroup, NavItemLink } from '@cypress-design/constants-docmenu'
3
- import { DocLink } from './_DocLink'
4
- import { DocGroup } from './_DocGroup'
5
- import clsx from 'clsx'
3
+ import type { LinkComponentType } from './_DocLink'
4
+ import { DocGroupElements } from './_DocGroup'
6
5
 
7
6
  export type NavItem = NavGroup | NavItemLink
8
7
 
9
8
  export interface DocMenuProps extends React.HTMLAttributes<HTMLUListElement> {
10
9
  items: NavItem[]
10
+ activePath?: string
11
11
  collapsible?: boolean
12
+ LinkComponent?: LinkComponentType
12
13
  }
13
14
 
14
15
  export const DocMenu: React.FC<DocMenuProps> = ({
15
16
  items,
17
+ activePath = '<unknown>',
16
18
  collapsible = true,
19
+ LinkComponent = 'a',
17
20
  ...rest
18
21
  }) => {
22
+ const [activeTop, setActiveTop] = React.useState(0)
23
+ const [activeHeight, setActiveHeight] = React.useState(36)
24
+ const [showMarker, setShowMarker] = React.useState(false)
25
+
26
+ const container = React.useRef<HTMLDivElement>(null)
27
+
28
+ const setActivePosition = React.useCallback(
29
+ (opts: { top: number; height: number }) => {
30
+ const containerTop = container.current?.getBoundingClientRect().top || 0
31
+ setShowMarker(true)
32
+ setActiveTop(opts.top - containerTop)
33
+ setActiveHeight(opts.height)
34
+ },
35
+ [],
36
+ )
37
+
19
38
  return (
20
- <ul {...rest} className={clsx('list-none p-0', rest.className)}>
21
- {items.map((item, index) =>
22
- 'items' in item ? (
23
- <li key={index} className="relative list-none p-0">
24
- <DocGroup group={item} collapsible={collapsible} />
25
- </li>
26
- ) : (
27
- <DocLink key={index} item={item} collapsible={collapsible} />
28
- )
29
- )}
30
- </ul>
39
+ <div ref={container} className="relative">
40
+ {showMarker && collapsible ? (
41
+ <div
42
+ className="absolute h-[36px] w-[4px] z-50 rounded-full bg-indigo-500 transition-all duration-300 ml-[6.5px] mt-[4px]"
43
+ style={{
44
+ top: `${activeTop + 4}px`,
45
+ height: `${activeHeight - 8}px`,
46
+ }}
47
+ />
48
+ ) : null}
49
+ <DocGroupElements
50
+ {...rest}
51
+ className={!open ? 'hidden' : undefined}
52
+ items={items}
53
+ activePath={activePath}
54
+ collapsible={collapsible}
55
+ depth={-1}
56
+ onActivePosition={setActivePosition}
57
+ LinkComponent={LinkComponent}
58
+ hideMarker={() => setShowMarker(false)}
59
+ />
60
+ </div>
31
61
  )
32
62
  }
33
63
 
@@ -2,13 +2,165 @@
2
2
 
3
3
  import * as React from 'react'
4
4
  import { mount } from 'cypress/react18'
5
- import DocMenuStory from './DocMenu.rootstory'
5
+ import DocMenu from './index'
6
6
  import assertions from '../assertions'
7
7
  import { NavGroup, NavItemLink } from '../constants'
8
8
 
9
9
  describe('<DocMenu/>', () => {
10
- function mountStory(items: (NavItemLink | NavGroup)[] = []) {
11
- mount(<DocMenuStory items={items} />)
10
+ it('works with 1 level of items', () => {
11
+ mount(<DocMenu items={[{ href: '/foo', label: 'Foo' }]} />)
12
+ })
13
+
14
+ it('works with 2 level of items', () => {
15
+ mount(
16
+ <DocMenu
17
+ items={[{ label: 'Bar', items: [{ href: '/foo', label: 'Foo' }] }]}
18
+ />,
19
+ )
20
+ })
21
+
22
+ it('works with 3 level of items', () => {
23
+ mount(
24
+ <DocMenu
25
+ items={[
26
+ {
27
+ label: 'Baaaaaaz',
28
+ items: [
29
+ {
30
+ label: 'Bar',
31
+ items: [{ href: '/foo', label: 'Foo' }],
32
+ },
33
+ ],
34
+ },
35
+ ]}
36
+ />,
37
+ )
38
+ })
39
+
40
+ it(
41
+ 'wraps items correctly',
42
+ { viewportHeight: 950, viewportWidth: 350 },
43
+ () => {
44
+ const Sut = () => {
45
+ const [activeHref, setActiveHref] = React.useState<string | undefined>()
46
+
47
+ return (
48
+ <div className="w-[200px] mt-[60px] absolute">
49
+ <DocMenu
50
+ activePath={activeHref}
51
+ LinkComponent={({ href, className, children }) => (
52
+ <div className={className} onClick={() => setActiveHref(href)}>
53
+ {children}
54
+ </div>
55
+ )}
56
+ items={[
57
+ {
58
+ href: '/fooTop',
59
+ label: 'Foo Top',
60
+ },
61
+ {
62
+ href: '/test',
63
+ label: 'Getting started',
64
+ },
65
+ {
66
+ href: '/faaz',
67
+ label:
68
+ 'lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam,',
69
+ },
70
+ {
71
+ label: 'Baaaaaaz',
72
+ items: [
73
+ {
74
+ label: 'Bar',
75
+ items: [
76
+ {
77
+ href: '/foo',
78
+ label: 'Foo',
79
+ },
80
+ {
81
+ href: '/test',
82
+ label: 'Getting started',
83
+ },
84
+ ],
85
+ },
86
+ {
87
+ label: 'Code',
88
+ collapsed: true,
89
+ items: [
90
+ {
91
+ href: '/faa',
92
+ label: 'sasassa',
93
+ },
94
+ {
95
+ href: '/faa1',
96
+ label: 'sasassa2',
97
+ },
98
+ {
99
+ href: '/faa2',
100
+ label: 'sasassa3',
101
+ },
102
+ {
103
+ href: '/faa3',
104
+ label: 'sasassa4',
105
+ },
106
+ {
107
+ href: '/faa4',
108
+ label:
109
+ 'lorem ipsum dolor sit amet consectetur adipisicing elit',
110
+ },
111
+ {
112
+ href: '/faa5',
113
+ label: 'sasassa5',
114
+ },
115
+ {
116
+ href: '/faa6',
117
+ label:
118
+ 'lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos',
119
+ },
120
+ ],
121
+ },
122
+ ],
123
+ },
124
+ ]}
125
+ />
126
+ </div>
127
+ )
128
+ }
129
+ mount(<Sut />)
130
+
131
+ cy.findByText('Bar').click()
132
+ cy.findByText('Code').click()
133
+ cy.findByText(
134
+ 'lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos',
135
+ ).click()
136
+ },
137
+ )
138
+
139
+ it('renders the custom link components', () => {
140
+ mount(
141
+ <DocMenu
142
+ items={[
143
+ {
144
+ label: 'Baaaaaaz',
145
+ items: [{ label: 'Bar', items: [{ href: '/foo', label: 'Foo' }] }],
146
+ },
147
+ ]}
148
+ LinkComponent={({ href, className, children }) => (
149
+ <div className={className}>
150
+ {children} + href: {href}
151
+ </div>
152
+ )}
153
+ />,
154
+ )
155
+
156
+ cy.findByText('Foo + href: /foo', { selector: 'div' }).should('be.visible')
157
+ })
158
+
159
+ function mountStory(items: (NavItemLink | NavGroup)[] = [], activePath = '') {
160
+ mount(<DocMenu items={items} activePath={activePath} />)
12
161
  }
13
162
  assertions(mountStory)
14
163
  })
164
+
165
+ // pseudo text
166
+ // lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quos
package/ReadMe.md CHANGED
@@ -18,62 +18,95 @@ yarn add @cypress-design/react-docmenu
18
18
  import DocMenu from '@cypress-design/react-docmenu'
19
19
  ```
20
20
 
21
+ ### Default usage
22
+
23
+ ```tsx live
24
+ export default () => (
25
+ <DocMenu
26
+ activePath="#group-item-3"
27
+ items={[
28
+ {
29
+ label: 'Get Started',
30
+ href: '#get-started',
31
+ },
32
+ {
33
+ label: 'Overview',
34
+ items: [
35
+ {
36
+ label: 'Overview Item 1',
37
+ href: '#overview-item-1',
38
+ },
39
+ {
40
+ label: 'Overview Item 2',
41
+ href: '#overview-item-2',
42
+ },
43
+ ],
44
+ },
45
+ {
46
+ label: 'Getting Started',
47
+ items: [
48
+ {
49
+ label: 'Item 1',
50
+ href: '#item-1',
51
+ },
52
+ {
53
+ label: 'Group',
54
+ items: [
55
+ {
56
+ label: 'Item 1',
57
+ href: '#group-item-1',
58
+ },
59
+ {
60
+ label: 'Item 2',
61
+ href: '#group-item-2',
62
+ },
63
+ {
64
+ label: 'Item 3',
65
+ href: '#group-item-3',
66
+ },
67
+ {
68
+ label: 'Item 4',
69
+ href: '#group-item-4',
70
+ },
71
+ ],
72
+ },
73
+ {
74
+ label: 'Item 5',
75
+ href: '#',
76
+ },
77
+ ],
78
+ },
79
+ ]}
80
+ />
81
+ )
82
+ ```
83
+
84
+ ### With a custom link
85
+
21
86
  ```tsx live
22
- <DocMenu
23
- items={[
24
- {
25
- text: 'Get Started',
26
- href: '#',
27
- },
28
- {
29
- text: 'Overview',
30
- items: [
31
- {
32
- text: 'Overview Item 1',
33
- href: '#',
34
- },
35
- {
36
- text: 'Overview Item 2',
37
- href: '#',
38
- active: true,
39
- },
40
- ],
41
- },
42
- {
43
- text: 'Getting Started',
44
- items: [
45
- {
46
- text: 'Item 1',
47
- href: '#',
48
- },
49
- {
50
- text: 'Group',
51
- items: [
52
- {
53
- text: 'Item 1',
54
- href: '#',
55
- },
56
- {
57
- text: 'Item 2',
58
- href: '#',
59
- },
60
- {
61
- text: 'Item 3',
62
- href: '#',
63
- },
64
- {
65
- text: 'Item 4',
66
- href: '#',
67
- },
68
- ],
69
- },
70
- {
71
- text: 'Item 5',
72
- href: '#',
73
- active: true,
74
- },
75
- ],
76
- },
77
- ]}
78
- />
87
+ const CustomLink = ({ href, children, className }) => {
88
+ return (
89
+ <a
90
+ href={href}
91
+ className={className}
92
+ onClick={(evt) => {
93
+ console.log('The link was clicked.', { target: evt.target })
94
+ }}
95
+ >
96
+ {children} 🔗
97
+ </a>
98
+ )
99
+ }
100
+
101
+ export default () => (
102
+ <DocMenu
103
+ LinkComponent={CustomLink}
104
+ items={[
105
+ {
106
+ label: 'Install',
107
+ href: '#with-a-custom-link',
108
+ },
109
+ ]}
110
+ />
111
+ )
79
112
  ```