@neovici/cosmoz-tabs 6.2.0 → 7.0.0
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 +3 -2
- package/src/use-tab.js +26 -17
- package/src/use-tabs.js +37 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neovici/cosmoz-tabs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "A multi views container element that allow navigation between the views using tabs or an accordion.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web-components"
|
|
@@ -68,10 +68,11 @@
|
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
70
|
"@neovici/cosmoz-router": "^10.0.0",
|
|
71
|
-
"@neovici/cosmoz-utils": "^
|
|
71
|
+
"@neovici/cosmoz-utils": "^5.0.0",
|
|
72
72
|
"@polymer/iron-icon": "^3.0.0",
|
|
73
73
|
"@polymer/iron-icons": "^3.0.0",
|
|
74
74
|
"compute-scroll-into-view": "^1.0.17",
|
|
75
|
+
"eslint-import-resolver-typescript": "^3.2.7",
|
|
75
76
|
"haunted": "^5.0.0",
|
|
76
77
|
"lit-html": "^2.0.0"
|
|
77
78
|
},
|
package/src/use-tab.js
CHANGED
|
@@ -1,26 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from 'haunted';
|
|
4
|
-
import { useNotifyProperty } from '@neovici/cosmoz-utils/lib/hooks/use-notify-property';
|
|
1
|
+
import { useEffect, useCallback } from 'haunted';
|
|
2
|
+
import { useNotifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property';
|
|
5
3
|
|
|
6
|
-
const useTab = host => {
|
|
4
|
+
const useTab = (host) => {
|
|
7
5
|
useEffect(() => {
|
|
8
|
-
host.dispatchEvent(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
host.dispatchEvent(
|
|
7
|
+
new CustomEvent('cosmoz-tab-alter', {
|
|
8
|
+
bubbles: true,
|
|
9
|
+
composed: true,
|
|
10
|
+
})
|
|
11
|
+
);
|
|
12
|
+
}, [
|
|
13
|
+
host.hidden,
|
|
14
|
+
host.disabled,
|
|
15
|
+
host.heading,
|
|
16
|
+
host.badge,
|
|
17
|
+
host.icon,
|
|
18
|
+
host.iconStyle,
|
|
19
|
+
]);
|
|
13
20
|
|
|
14
21
|
useNotifyProperty('isSelected', host.isSelected);
|
|
15
22
|
|
|
16
23
|
return {
|
|
17
|
-
onSlot: useCallback(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
onSlot: useCallback(
|
|
25
|
+
({ target }) =>
|
|
26
|
+
host.toggleAttribute(
|
|
27
|
+
'has-cards',
|
|
28
|
+
target.assignedElements().some((el) => el.matches('cosmoz-tab-card'))
|
|
29
|
+
),
|
|
30
|
+
[]
|
|
31
|
+
),
|
|
21
32
|
};
|
|
22
33
|
};
|
|
23
34
|
|
|
24
|
-
export {
|
|
25
|
-
useTab
|
|
26
|
-
};
|
|
35
|
+
export { useTab };
|
package/src/use-tabs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState, useEffect, useMemo, useCallback } from 'haunted';
|
|
2
|
-
import { notifyProperty } from '@neovici/cosmoz-utils/
|
|
2
|
+
import { notifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property';
|
|
3
3
|
/* eslint-disable-next-line import/no-unresolved */
|
|
4
4
|
import { useHashParam, link } from '@neovici/cosmoz-router/use-hash-param';
|
|
5
5
|
import { choose, collect, getName, isValid } from './utils';
|
|
@@ -18,7 +18,9 @@ const useTabSelectedEffect = (host, selectedTab) => {
|
|
|
18
18
|
selectedTab.toggleAttribute('is-selected', true);
|
|
19
19
|
const eventOpts = { composed: true };
|
|
20
20
|
if (!selectedTab._active) {
|
|
21
|
-
selectedTab.dispatchEvent(
|
|
21
|
+
selectedTab.dispatchEvent(
|
|
22
|
+
new CustomEvent('tab-first-select', eventOpts)
|
|
23
|
+
);
|
|
22
24
|
selectedTab._active = true;
|
|
23
25
|
}
|
|
24
26
|
selectedTab.dispatchEvent(new CustomEvent('tab-select', eventOpts));
|
|
@@ -29,41 +31,48 @@ const useTabSelectedEffect = (host, selectedTab) => {
|
|
|
29
31
|
selectedTab.toggleAttribute('is-selected', false);
|
|
30
32
|
selectedTab._fallbackFor = undefined;
|
|
31
33
|
};
|
|
32
|
-
|
|
33
34
|
}, [selectedTab]);
|
|
34
35
|
},
|
|
35
|
-
|
|
36
36
|
useAutoScroll = (host, selectedTab) => {
|
|
37
37
|
useEffect(() => {
|
|
38
38
|
const el = host.shadowRoot.querySelector('a[aria-selected]');
|
|
39
39
|
if (!el) {
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
|
-
computeScroll(el, {
|
|
43
|
-
|
|
42
|
+
computeScroll(el, {
|
|
43
|
+
block: 'nearest',
|
|
44
|
+
inline: 'center',
|
|
45
|
+
boundary: el.parentElement,
|
|
46
|
+
}).forEach(({ el, top, left }) =>
|
|
47
|
+
el.scroll({ top, left, behavior: 'smooth' })
|
|
48
|
+
);
|
|
44
49
|
}, [selectedTab]);
|
|
45
50
|
},
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const {
|
|
49
|
-
selected, hashParam
|
|
50
|
-
} = host,
|
|
51
|
+
useTabs = (host) => {
|
|
52
|
+
const { selected, hashParam } = host,
|
|
51
53
|
[tabs, setTabs] = useState([]),
|
|
52
54
|
[param] = useHashParam(hashParam),
|
|
53
|
-
selection =
|
|
55
|
+
selection =
|
|
56
|
+
hashParam == null || (param == null && selected != null)
|
|
57
|
+
? selected
|
|
58
|
+
: param,
|
|
54
59
|
selectedTab = useMemo(() => choose(tabs, selection), [tabs, selection]);
|
|
55
60
|
|
|
56
61
|
useTabSelectedEffect(host, selectedTab);
|
|
57
62
|
|
|
58
63
|
useEffect(() => {
|
|
59
|
-
const onTabAlter = e => {
|
|
64
|
+
const onTabAlter = (e) => {
|
|
60
65
|
e.stopPropagation();
|
|
61
66
|
const { target: tab } = e;
|
|
62
|
-
if (
|
|
67
|
+
if (
|
|
68
|
+
selectedTab != null &&
|
|
69
|
+
selectedTab._fallbackFor === tab &&
|
|
70
|
+
isValid(tab)
|
|
71
|
+
) {
|
|
63
72
|
selectedTab._fallbackFor = undefined;
|
|
64
73
|
host.selected = getName(tab);
|
|
65
74
|
}
|
|
66
|
-
setTabs(prev => prev.slice());
|
|
75
|
+
setTabs((prev) => prev.slice());
|
|
67
76
|
};
|
|
68
77
|
host.addEventListener('cosmoz-tab-alter', onTabAlter);
|
|
69
78
|
return () => host.removeEventListener('cosmoz-tab-alter', onTabAlter);
|
|
@@ -71,13 +80,19 @@ const useTabSelectedEffect = (host, selectedTab) => {
|
|
|
71
80
|
|
|
72
81
|
useAutoScroll(host, selectedTab);
|
|
73
82
|
|
|
74
|
-
const href = useCallback(
|
|
83
|
+
const href = useCallback(
|
|
84
|
+
(tab) => (isValid(tab) ? link(hashParam, getName(tab)) : undefined),
|
|
85
|
+
[hashParam]
|
|
86
|
+
);
|
|
75
87
|
|
|
76
88
|
return {
|
|
77
89
|
tabs,
|
|
78
90
|
selectedTab,
|
|
79
|
-
onSlot: useCallback(
|
|
80
|
-
|
|
91
|
+
onSlot: useCallback(
|
|
92
|
+
({ target }) => requestAnimationFrame(() => setTabs(collect(target))),
|
|
93
|
+
[]
|
|
94
|
+
),
|
|
95
|
+
onSelect: useCallback((e) => {
|
|
81
96
|
if (e.button !== 0 || e.metaKey || e.ctrlKey) {
|
|
82
97
|
return;
|
|
83
98
|
}
|
|
@@ -90,9 +105,11 @@ const useTabSelectedEffect = (host, selectedTab) => {
|
|
|
90
105
|
|
|
91
106
|
e.preventDefault();
|
|
92
107
|
window.history.pushState({}, '', href(tab));
|
|
93
|
-
requestAnimationFrame(() =>
|
|
108
|
+
requestAnimationFrame(() =>
|
|
109
|
+
window.dispatchEvent(new CustomEvent('hashchange'))
|
|
110
|
+
);
|
|
94
111
|
}, []),
|
|
95
|
-
href
|
|
112
|
+
href,
|
|
96
113
|
};
|
|
97
114
|
};
|
|
98
115
|
|