@veritree/ui 0.0.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.
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import vtAccordion from './src/vtAccordion/vtAccordion.vue';
2
+ import vtAccordionButton from './src/vtAccordion/vtAccordionButton.vue';
3
+ import vtAccordionGroup from './src/vtAccordion/vtAccordionGroup.vue';
4
+ import vtAccordionPanel from './src/vtAccordion/vtAccordionPanel.vue';
5
+
6
+ export {
7
+ vtAccordion,
8
+ vtAccordionButton,
9
+ vtAccordionGroup,
10
+ vtAccordionPanel,
11
+ }
12
+
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@veritree/ui",
3
+ "version": "0.0.1",
4
+ "description": "veritree ui library",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "repository": "https://github.com/tentree-org/veritree-ui.git",
8
+ "author": "jb",
9
+ "license": "MIT",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ }
13
+ }
@@ -0,0 +1,14 @@
1
+ <template>
2
+ <svg
3
+ width="24"
4
+ height="24"
5
+ viewBox="0 0 24 24"
6
+ fill="none"
7
+ xmlns="http://www.w3.org/2000/svg"
8
+ >
9
+ <path
10
+ d="M16.59 8.59L12 13.17L7.41 8.59L6 10L12 16L18 10L16.59 8.59Z"
11
+ fill="currentColor"
12
+ />
13
+ </svg>
14
+ </template>
@@ -0,0 +1,14 @@
1
+ <template>
2
+ <svg
3
+ width="24"
4
+ height="24"
5
+ viewBox="0 0 24 24"
6
+ fill="none"
7
+ xmlns="http://www.w3.org/2000/svg"
8
+ >
9
+ <path
10
+ d="M12 8L6 14L7.41 15.41L12 10.83L16.59 15.41L18 14L12 8Z"
11
+ fill="currentColor"
12
+ />
13
+ </svg>
14
+ </template>
@@ -0,0 +1,13 @@
1
+ // Generate id
2
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
3
+ let gen = null;
4
+
5
+ function* idMaker() {
6
+ let index = 0;
7
+ while (true) yield index++;
8
+ }
9
+
10
+ export const genId = () => {
11
+ if (!gen) gen = idMaker();
12
+ return gen.next().value;
13
+ };
@@ -0,0 +1,10 @@
1
+ export const keys = {
2
+ left: 'ArrowLeft',
3
+ right: 'ArrowRight',
4
+ down: 'ArrowDown',
5
+ up: 'ArrowUp',
6
+ home: 'Home',
7
+ end: 'End',
8
+ enter: 'Enter',
9
+ escape: 'Escape',
10
+ };
@@ -0,0 +1,105 @@
1
+ <template>
2
+ <div class="accordion" :multiple="multiple"><slot></slot></div>
3
+ </template>
4
+
5
+ <script>
6
+ export default {
7
+ name: 'vtAccordion',
8
+ props: {
9
+ multiple: {
10
+ type: Boolean,
11
+ default: false
12
+ }
13
+ },
14
+ provide() {
15
+ return {
16
+ api: () => {
17
+ // Get children components
18
+ const getAccordion = () => this.accordion;
19
+ const getAccordionGroup = () => this.accordionGroup;
20
+ const getAccordionPanel = () => this.accordionPanel;
21
+ const getAccordionButton = () => this.accordionButton;
22
+
23
+ // Handle registering and unregistering
24
+ const registerAccordion = (accordion) => {
25
+ if (!accordion) return;
26
+ this.accordion = accordion;
27
+ };
28
+
29
+ const registerAccordionButton = (button) => {
30
+ if (!button) return;
31
+ this.accordionButton = button;
32
+ };
33
+
34
+ const registerAccordionGroup = (group) => {
35
+ if (!group) return;
36
+ _register(this.accordionGroup, group);
37
+ };
38
+
39
+ const registerAccordionPanel = (panel) => {
40
+ if (!panel) return;
41
+ _register(this.accordionPanel, panel);
42
+ };
43
+
44
+ const unregisterAccordionPanel = (id) => {
45
+ _unregister(this.accordionPanel, id);
46
+ };
47
+
48
+ const selectedPanel = (panelId) => {
49
+ if (this.multiple) {
50
+ this.accordionPanel.forEach((item) => {
51
+ if(item.id === panelId) item.multipleActive();
52
+ })
53
+ } else{
54
+ this.accordionPanel.forEach((item) => {
55
+ item.id === panelId ? item.show() : item.hide();
56
+ })
57
+ }
58
+ };
59
+
60
+ // Register helper functions
61
+ const _register = (arr, item) => {
62
+ arr.push(item);
63
+ _index(arr);
64
+ };
65
+
66
+ const _unregister = (arr, id) => {
67
+ const index = _getIndex(arr, id);
68
+ arr.splice(index, 1);
69
+ };
70
+
71
+ // Index helper functions
72
+ const _index = (arr) => {
73
+ arr.forEach((item, index) => (item.index = index));
74
+ };
75
+
76
+ const _getIndex = (arr, id) => {
77
+ return arr.findIndex((item) => item.id === id);
78
+ };
79
+
80
+ return {
81
+ getAccordion,
82
+ getAccordionGroup,
83
+ getAccordionPanel,
84
+ getAccordionButton,
85
+ registerAccordionGroup,
86
+ registerAccordionButton,
87
+ registerAccordion,
88
+ registerAccordionPanel,
89
+ unregisterAccordionPanel,
90
+ selectedPanel,
91
+ };
92
+ },
93
+ };
94
+ },
95
+
96
+ data() {
97
+ return {
98
+ accordion: null,
99
+ accordionGroup: [],
100
+ accordionButton: null,
101
+ accordionPanel: [],
102
+ };
103
+ },
104
+ };
105
+ </script>
@@ -0,0 +1,56 @@
1
+ <template>
2
+ <h3>
3
+ <button
4
+ :id="id"
5
+ :aria-expanded="String(expanded)"
6
+ :aria-controls="id"
7
+ class="accordion-trigger"
8
+ type="button"
9
+ @click.prevent="onClick"
10
+ >
11
+ <span class="Accordion-button__text"><slot></slot></span>
12
+ <span class="Accordion-button__icon">
13
+ <IconChevronUp v-if="expanded" />
14
+ <IconChevronDown v-else />
15
+ </span>
16
+ </button>
17
+ </h3>
18
+ </template>
19
+
20
+ <script>
21
+ import IconChevronDown from '../icons/IconChevronDown.vue';
22
+ import IconChevronUp from '../icons/IconChevronUp.vue';
23
+
24
+ export default {
25
+ name: 'vtAccordionButton',
26
+ components: { IconChevronDown, IconChevronUp },
27
+ inject: ['api'],
28
+
29
+ data() {
30
+ return {
31
+ api: this.api(),
32
+ expanded: false,
33
+ };
34
+ },
35
+
36
+ computed: {
37
+ id() {
38
+ return this.$parent.id;
39
+ },
40
+ },
41
+
42
+ mounted() {
43
+ this.api.registerAccordionButton(this);
44
+ },
45
+
46
+ methods: {
47
+ toggleExpanded() {
48
+ this.expanded = !this.expanded;
49
+ },
50
+ onClick() {
51
+ const accordion = this.api.selectedPanel(this.id);
52
+ this.toggleExpanded();
53
+ },
54
+ },
55
+ };
56
+ </script>
@@ -0,0 +1,29 @@
1
+ <template>
2
+ <div
3
+ :id="id"
4
+ class="accordion-group"
5
+ >
6
+ <slot></slot>
7
+ </div>
8
+ </template>
9
+
10
+ <script>
11
+ import { genId } from '../utils/genId';
12
+
13
+ export default {
14
+ name: 'vtAccordionGroup',
15
+ inject: ['api'],
16
+
17
+ data() {
18
+ return {
19
+ api: this.api(),
20
+ id: `group-${genId()}`,
21
+ };
22
+ },
23
+
24
+ mounted() {
25
+ this.api.registerAccordionGroup(this);
26
+ },
27
+
28
+ };
29
+ </script>
@@ -0,0 +1,55 @@
1
+ <template>
2
+ <div
3
+ :id="id"
4
+ :aria-labelledby="id"
5
+ v-show="active"
6
+ class="accordion-panel"
7
+ role="region"
8
+ >
9
+ <slot></slot>
10
+ </div>
11
+ </template>
12
+
13
+ <script>
14
+ // import { keys } from '@/utils/keyboard';
15
+
16
+ export default {
17
+ name: 'vtAccordionPanel',
18
+ inject: ['api'],
19
+
20
+ data() {
21
+ return {
22
+ api: this.api(),
23
+ active: false,
24
+ };
25
+ },
26
+
27
+ computed: {
28
+ id() {
29
+ return this.$parent.id;
30
+ },
31
+ },
32
+
33
+ mounted() {
34
+ this.api.registerAccordionPanel(this);
35
+ },
36
+
37
+ methods: {
38
+ multipleActive(){
39
+ this.active = !this.active;
40
+ },
41
+
42
+ show() {
43
+ if(this.active) {
44
+ this.active = false
45
+ } else {
46
+ this.active = true
47
+ }
48
+ },
49
+
50
+ hide() {
51
+ this.active = false;
52
+ },
53
+ },
54
+ };
55
+ </script>