graphen 2.0.7 → 2.0.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphen",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "Graphen is a small library, that keeps reusable blocks of UI and helps making application design consistent across multiple projects.",
5
5
  "main": "src/index.ts",
6
6
  "files": [
@@ -1,6 +1,6 @@
1
1
  describe("dropdown menu", () => {
2
2
  it("should open dropdown and pick a value", () => {
3
- cy.visit("http://localhost:3000");
3
+ cy.visit("/");
4
4
 
5
5
  cy.get("#dropdown").scrollIntoView();
6
6
 
@@ -0,0 +1,19 @@
1
+ import React from "react";
2
+ import classNames from "classnames";
3
+
4
+ type Props = {
5
+ className?: string;
6
+ children?: React.ReactNode;
7
+ };
8
+
9
+ function Navigation({ className = "", children = null }: Props) {
10
+ const navigationClasses = classNames(
11
+ className,
12
+ "gc-navigation",
13
+ "gc-navigation--default"
14
+ );
15
+
16
+ return <ul className={navigationClasses}>{children}</ul>;
17
+ }
18
+
19
+ export default Navigation;
@@ -7,24 +7,21 @@
7
7
 
8
8
  &__option {
9
9
  display: inline-block;
10
- position: relative;
11
10
  border-bottom: 3px solid transparent;
12
11
 
13
12
  &--active {
14
13
  border-color: $gb-color-primary;
15
14
  }
16
-
17
- &:hover {
18
- .gc-submenu {
19
- display: block;
20
- }
21
- }
22
15
  }
23
16
 
24
17
  &__link {
25
18
  display: block;
26
- padding-left: $spacing-large;
27
- padding-right: $spacing-large;
19
+ padding: 0 $spacing-large;
20
+ border: 0;
21
+ background: none;
22
+ color: inherit;
23
+ cursor: pointer;
24
+ font: inherit;
28
25
  line-height: 54px;
29
26
  font-weight: 600;
30
27
  }
@@ -1,14 +1,11 @@
1
1
  .gc-submenu {
2
2
  display: none;
3
- position: absolute;
4
- top: 100%;
5
- left: 0;
3
+ position: fixed;
6
4
  background-color: transparent;
7
5
  z-index: $zlayer-top;
8
6
 
9
- @include gx-phone {
10
- left: auto;
11
- right: 0;
7
+ &--opened {
8
+ display: block;
12
9
  }
13
10
 
14
11
  &__content {
@@ -0,0 +1,131 @@
1
+ import React, { useCallback, useLayoutEffect, useRef, useState } from "react";
2
+ import classNames from "classnames";
3
+
4
+ const SUBMENU_VIEWPORT_MARGIN = 10;
5
+
6
+ type Props = {
7
+ className?: string;
8
+ label: React.ReactNode;
9
+ href?: string;
10
+ isActive?: boolean;
11
+ children?: React.ReactNode;
12
+ };
13
+
14
+ function NavigationOption({
15
+ className = "",
16
+ label,
17
+ href = undefined,
18
+ isActive = false,
19
+ children = null,
20
+ }: Props) {
21
+ const [isOpened, setIsOpened] = useState(false);
22
+ const [submenuPosition, setSubmenuPosition] = useState({ top: 0, left: 0 });
23
+ const triggerReference = useRef<HTMLButtonElement>(null);
24
+ const submenuReference = useRef<HTMLDivElement>(null);
25
+
26
+ const toggleSubmenu = useCallback(() => {
27
+ setIsOpened((previousIsOpened) => !previousIsOpened);
28
+ }, [setIsOpened]);
29
+
30
+ const updateSubmenuPosition = useCallback(() => {
31
+ const trigger = triggerReference.current;
32
+ const submenu = submenuReference.current;
33
+
34
+ if (!trigger || !submenu) {
35
+ return;
36
+ }
37
+
38
+ const triggerRect = trigger.getBoundingClientRect();
39
+ const maximumLeft =
40
+ window.innerWidth - submenu.offsetWidth - SUBMENU_VIEWPORT_MARGIN;
41
+
42
+ setSubmenuPosition({
43
+ top: triggerRect.bottom,
44
+ left: Math.max(
45
+ SUBMENU_VIEWPORT_MARGIN,
46
+ Math.min(triggerRect.left, maximumLeft)
47
+ ),
48
+ });
49
+ }, [setSubmenuPosition]);
50
+
51
+ useLayoutEffect(() => {
52
+ if (!isOpened) {
53
+ return undefined;
54
+ }
55
+
56
+ updateSubmenuPosition();
57
+
58
+ // capture phase catches scrolling inside consumer overflow containers
59
+ window.addEventListener("scroll", updateSubmenuPosition, true);
60
+ window.addEventListener("resize", updateSubmenuPosition);
61
+
62
+ return () => {
63
+ window.removeEventListener("scroll", updateSubmenuPosition, true);
64
+ window.removeEventListener("resize", updateSubmenuPosition);
65
+ };
66
+ }, [isOpened, updateSubmenuPosition]);
67
+
68
+ const closeSubmenuOnBlur = useCallback(
69
+ (event: React.FocusEvent<HTMLLIElement>) => {
70
+ if (!event.currentTarget.contains(event.relatedTarget as Node | null)) {
71
+ setIsOpened(false);
72
+ }
73
+ },
74
+ [setIsOpened]
75
+ );
76
+
77
+ const closeSubmenuOnEscape = useCallback(
78
+ (event: React.KeyboardEvent<HTMLLIElement>) => {
79
+ if (event.key === "Escape") {
80
+ setIsOpened(false);
81
+ }
82
+ },
83
+ [setIsOpened]
84
+ );
85
+
86
+ const optionClasses = classNames(className, "gc-navigation__option", {
87
+ "gc-navigation__option--active": isActive,
88
+ });
89
+
90
+ if (!children) {
91
+ return (
92
+ <li className={optionClasses}>
93
+ <a className="gc-navigation__link" href={href}>
94
+ {label}
95
+ </a>
96
+ </li>
97
+ );
98
+ }
99
+
100
+ const submenuClasses = classNames("gc-submenu", {
101
+ "gc-submenu--opened": isOpened,
102
+ });
103
+
104
+ return (
105
+ <li
106
+ className={optionClasses}
107
+ onBlur={closeSubmenuOnBlur}
108
+ onKeyDown={closeSubmenuOnEscape}
109
+ >
110
+ <button
111
+ ref={triggerReference}
112
+ type="button"
113
+ className="gc-navigation__link"
114
+ aria-haspopup="true"
115
+ aria-expanded={isOpened}
116
+ onClick={toggleSubmenu}
117
+ >
118
+ {label}
119
+ </button>
120
+ <div
121
+ ref={submenuReference}
122
+ className={submenuClasses}
123
+ style={{ top: submenuPosition.top, left: submenuPosition.left }}
124
+ >
125
+ <div className="gc-submenu__content">{children}</div>
126
+ </div>
127
+ </li>
128
+ );
129
+ }
130
+
131
+ export default NavigationOption;
@@ -0,0 +1,30 @@
1
+ describe("navigation submenu", () => {
2
+ it("should anchor submenu to its parent option in a scrolled navigation", () => {
3
+ cy.visit("/");
4
+
5
+ cy.get("#navigation").scrollIntoView();
6
+
7
+ cy.get("#navigation .gc-navigation")
8
+ .eq(1)
9
+ .parent()
10
+ .scrollTo("right")
11
+ .within(() => {
12
+ cy.contains("button.gc-navigation__link", "Platform").click();
13
+ });
14
+
15
+ cy.window().then((win) => {
16
+ cy.contains("button.gc-navigation__link", "Platform").then(($trigger) => {
17
+ const triggerRect = $trigger[0].getBoundingClientRect();
18
+
19
+ cy.get(".gc-submenu--opened").then(($submenu) => {
20
+ const submenuRect = $submenu[0].getBoundingClientRect();
21
+
22
+ expect(submenuRect.top).to.be.closeTo(triggerRect.bottom, 1);
23
+ expect(submenuRect.left).to.be.at.most(triggerRect.left + 1);
24
+ expect(submenuRect.left).to.be.at.least(0);
25
+ expect(submenuRect.right).to.be.at.most(win.innerWidth);
26
+ });
27
+ });
28
+ });
29
+ });
30
+ });
@@ -605,6 +605,63 @@ body.docs-body {
605
605
  overflow: hidden;
606
606
  }
607
607
 
608
+ .docs-flex-demo {
609
+ width: 100%;
610
+ }
611
+
612
+ .docs-flex-box {
613
+ padding: 10px 16px;
614
+ border: 1px solid var(--border);
615
+ border-radius: var(--radius);
616
+ background: var(--surface-2);
617
+ font-size: 13px;
618
+ color: var(--text);
619
+ }
620
+
621
+ .docs-loader-stage {
622
+ display: flex;
623
+ align-items: center;
624
+ justify-content: center;
625
+ width: 120px;
626
+ height: 96px;
627
+ border-radius: var(--radius-lg);
628
+ background: #23272e;
629
+ }
630
+
631
+ .docs-skeleton-demo {
632
+ width: 260px;
633
+
634
+ .gc-skeleton:nth-child(2) {
635
+ width: 80%;
636
+ }
637
+
638
+ .gc-skeleton:nth-child(3) {
639
+ width: 55%;
640
+ }
641
+ }
642
+
643
+ .docs-tooltip-anchor {
644
+ position: relative;
645
+ width: 180px;
646
+ height: 56px;
647
+ }
648
+
649
+ .docs-list-demo {
650
+ width: 100%;
651
+ max-width: 360px;
652
+ margin: 0;
653
+ }
654
+
655
+ .docs-scroller-track {
656
+ width: 240px;
657
+ }
658
+
659
+ .docs-demo-value {
660
+ font-family: var(--font-mono);
661
+ font-size: 12.5px;
662
+ color: var(--text-muted);
663
+ }
664
+
608
665
  .docs-type-row {
609
666
  display: grid;
610
667
  grid-template-columns: 80px 80px 1fr;