@veritree/ui 0.0.1 → 0.1.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.
@@ -0,0 +1,121 @@
1
+ <template>
2
+ <li
3
+ :id="id"
4
+ :aria-selected="selected"
5
+ class="Listbox-option"
6
+ role="option"
7
+ @mousedown.prevent="onMouseDown"
8
+ @mousemove="onMousemove"
9
+ @mouseout="onMouseleave"
10
+ >
11
+ <slot></slot>
12
+ </li>
13
+ </template>
14
+
15
+ <script>
16
+ import { genId } from '../utils/ids';
17
+ import { areObjsEqual, isObj } from '../utils/objects';
18
+
19
+ export default {
20
+ name: 'VTListboxOption',
21
+
22
+ inject: ['api'],
23
+
24
+ props: {
25
+ value: {
26
+ type: [String, Number, Object],
27
+ required: true,
28
+ },
29
+ },
30
+
31
+ data() {
32
+ return {
33
+ api: this.api(),
34
+ id: `listbox-option-${genId()}`,
35
+ selected: false,
36
+ focused: false,
37
+ isMousemove: false,
38
+ };
39
+ },
40
+
41
+ watch: {
42
+ focused(newValue) {
43
+ if (newValue) {
44
+ if (!this.isMousemove) {
45
+ this.$el.scrollIntoView({ block: 'nearest' });
46
+ }
47
+
48
+ const listbox = this.api.getListbox();
49
+ listbox.updateActiveDescendant(this.id);
50
+ }
51
+ },
52
+ },
53
+
54
+ mounted() {
55
+ this.api.registerOption(this);
56
+
57
+ // todo: make sure it works with other values than objects
58
+ const listboxOptionSelected = this.api.getListboxValue();
59
+
60
+ if (!listboxOptionSelected) {
61
+ return;
62
+ }
63
+
64
+ if (isObj(this.value)) {
65
+ if (areObjsEqual(this.value, listboxOptionSelected)) {
66
+ this.select();
67
+ this.focus();
68
+ }
69
+
70
+ return;
71
+ }
72
+
73
+ if (this.value === listboxOptionSelected) {
74
+ this.select();
75
+ this.focus();
76
+ }
77
+ },
78
+
79
+ beforeDestroy() {
80
+ this.api.unregisterOption(this.id);
81
+ },
82
+
83
+ methods: {
84
+ focus() {
85
+ this.focused = true;
86
+ this.$el.classList.add('is-focused');
87
+ },
88
+
89
+ unfocus() {
90
+ this.focused = false;
91
+ this.$el.classList.remove('is-focused');
92
+ },
93
+
94
+ select() {
95
+ this.selected = true;
96
+ },
97
+
98
+ unselect() {
99
+ this.selected = false;
100
+ },
101
+
102
+ onMouseDown(event) {
103
+ if (event.buttons === 1) this.api.selectOption(this);
104
+ },
105
+
106
+ // Mousemove instead of mouseover to support keyboard navigation.
107
+ // The problem with mouseover is that when scrolling (scrollIntoView),
108
+ // mouseover event gets triggered.
109
+ onMousemove() {
110
+ this.isMousemove = true;
111
+ this.api.unfocusOptions();
112
+ this.focus();
113
+ },
114
+
115
+ onMouseleave() {
116
+ this.isMousemove = false;
117
+ this.unfocus();
118
+ },
119
+ },
120
+ };
121
+ </script>
@@ -0,0 +1,120 @@
1
+ <template>
2
+ <ul
3
+ v-if="visible"
4
+ aria-orientation="vertical"
5
+ role="listbox"
6
+ class="Listbox-options"
7
+ tabindex="-1"
8
+ @keydown="onKeyDown"
9
+ @blur="onBlur"
10
+ >
11
+ <slot></slot>
12
+ </ul>
13
+ </template>
14
+
15
+ <script>
16
+ import { keys } from '../utils/keyboard';
17
+ let timer = null;
18
+
19
+ export default {
20
+ name: 'VTListboxOptions',
21
+
22
+ inject: ['api'],
23
+
24
+ data() {
25
+ return {
26
+ api: this.api(),
27
+ visible: false,
28
+ filter: '',
29
+ };
30
+ },
31
+
32
+ watch: {
33
+ visible(newValue) {
34
+ if (newValue) {
35
+ this.$nextTick(() => {
36
+ this.$el.focus();
37
+ this.handleOptionFocus();
38
+ });
39
+ } else {
40
+ const listboxButton = this.api.getListboxButton();
41
+
42
+ this.$nextTick(() => {
43
+ listboxButton.focus();
44
+ });
45
+ }
46
+ },
47
+ },
48
+
49
+ mounted() {
50
+ this.api.registerListbox(this);
51
+ },
52
+
53
+ methods: {
54
+ show() {
55
+ this.visible = true;
56
+ },
57
+
58
+ hide() {
59
+ this.visible = false;
60
+ },
61
+
62
+ handleOptionFocus() {
63
+ const selectedIndex = this.api.getFocusedIndex();
64
+ if (!selectedIndex) this.api.focusFirstOption();
65
+ },
66
+
67
+ updateActiveDescendant(id) {
68
+ this.$el.setAttribute('aria-activedescendant', id);
69
+ },
70
+
71
+ onKeyDown(event) {
72
+ const key = event.key;
73
+ const code = event.code;
74
+
75
+ if (code.includes('Key')) {
76
+ this.filter += code.replace('Key', '');
77
+
78
+ clearTimeout(timer);
79
+
80
+ timer = setTimeout(() => {
81
+ this.api.focusOptionByFilter(this.filter);
82
+ this.filter = '';
83
+ }, 300);
84
+ }
85
+
86
+ switch (key) {
87
+ case keys.up:
88
+ event.preventDefault();
89
+ this.api.focusPreviousOption();
90
+ break;
91
+ case keys.down:
92
+ event.preventDefault();
93
+ this.api.focusNextOption();
94
+ break;
95
+ case keys.home:
96
+ event.preventDefault();
97
+ this.api.focusFirstOption();
98
+ break;
99
+ case keys.end:
100
+ event.preventDefault();
101
+ this.api.focusLastOption();
102
+ break;
103
+ case keys.enter:
104
+ event.preventDefault();
105
+ this.api.selectOption();
106
+ break;
107
+ case keys.escape:
108
+ this.hide();
109
+ break;
110
+ default:
111
+ break;
112
+ }
113
+ },
114
+
115
+ onBlur() {
116
+ this.hide();
117
+ },
118
+ },
119
+ };
120
+ </script>
@@ -0,0 +1,69 @@
1
+ <template>
2
+ <transition
3
+ enter-active-class="duration-300 ease-out"
4
+ enter-class="transform opacity-0"
5
+ enter-to-class="opacity-100"
6
+ leave-active-class="duration-300 ease-in"
7
+ leave-class="opacity-100"
8
+ leave-to-class="transform opacity-0"
9
+ @after-enter="afterEnter"
10
+ @after-leave="afterLeave"
11
+ >
12
+ <div
13
+ v-if="visible"
14
+ class="fixed inset-0 z-50 flex flex-col justify-center bg-fd-700/75 px-4 sm:px-6 md:px-8"
15
+ tabindex="-1"
16
+ @keyup.esc="close"
17
+ @click="close"
18
+ >
19
+ <div
20
+ class="relative mx-auto flex max-w-lg flex-col justify-center rounded bg-white p-20"
21
+ @click.stop
22
+ >
23
+ <div class="absolute right-4 top-4">
24
+ <VTButton variant="icon" @click.prevent="close"
25
+ ><IconClose class="h-4 w-4"
26
+ /></VTButton>
27
+ </div>
28
+ <slot></slot>
29
+ </div>
30
+ </div>
31
+ </transition>
32
+ </template>
33
+
34
+ <script>
35
+ import { IconClose } from '@veritree/icons';
36
+ import VTButton from '../Button/VTButton.vue';
37
+
38
+ export default {
39
+ name: 'VTModal',
40
+
41
+ components: { IconClose, VTButton },
42
+
43
+ model: {
44
+ prop: 'visible',
45
+ },
46
+
47
+ props: {
48
+ visible: {
49
+ type: Boolean,
50
+ default: false,
51
+ },
52
+ },
53
+
54
+ methods: {
55
+ close() {
56
+ this.$emit('input', false);
57
+ },
58
+
59
+ afterEnter() {
60
+ this.$emit('shown');
61
+ this.$nextTick(() => this.$el.focus());
62
+ },
63
+
64
+ afterLeave() {
65
+ this.$emit('hidden');
66
+ },
67
+ },
68
+ };
69
+ </script>
@@ -0,0 +1,126 @@
1
+ <template>
2
+ <button
3
+ :id="id"
4
+ :aria-selected="selected"
5
+ :aria-controls="`tabpanel-${index}`"
6
+ role="tab"
7
+ type="button"
8
+ @click.stop="onClick"
9
+ @keydown="onKeyDown"
10
+ @blur="onBlur"
11
+ >
12
+ <slot></slot>
13
+ </button>
14
+ </template>
15
+
16
+ <script>
17
+ import { keys } from '../utils/keyboard';
18
+
19
+ export default {
20
+ name: 'VTTabItem',
21
+
22
+ inject: ['api'],
23
+
24
+ data() {
25
+ return {
26
+ api: this.api(),
27
+ index: null,
28
+ indexFocus: null,
29
+ selected: false,
30
+ };
31
+ },
32
+
33
+ computed: {
34
+ id() {
35
+ return `tab-${this.index}`;
36
+ },
37
+ },
38
+
39
+ mounted() {
40
+ this.api.registerTab(this);
41
+ },
42
+
43
+ beforeDestroy() {
44
+ this.api.unregisterTab(this.index);
45
+ },
46
+
47
+ methods: {
48
+ select() {
49
+ this.selected = true;
50
+ },
51
+
52
+ unselect() {
53
+ this.selected = false;
54
+ },
55
+
56
+ focus() {
57
+ this.$el.focus();
58
+ },
59
+
60
+ focusFirstTab() {
61
+ this.api.focusTab(0);
62
+ },
63
+
64
+ focusLastTab(lastTabIndex) {
65
+ this.api.focusTab(lastTabIndex);
66
+ },
67
+
68
+ focusPreviousTab(lastTabIndex) {
69
+ if (this.indexFocus === 0) {
70
+ this.focusLastTab(lastTabIndex);
71
+ return;
72
+ }
73
+
74
+ this.api.focusTab(this.indexFocus - 1);
75
+ },
76
+
77
+ focusNextTab(lastTabIndex) {
78
+ if (this.indexFocus === lastTabIndex) {
79
+ this.focusFirstTab();
80
+ return;
81
+ }
82
+
83
+ this.api.focusTab(this.indexFocus + 1);
84
+ },
85
+
86
+ onClick() {
87
+ this.api.selectTab(this.index);
88
+ this.$emit('change');
89
+ },
90
+
91
+ onKeyDown(event) {
92
+ const key = event.key;
93
+ const lastTabIndex = this.api.getTabsLength() - 1;
94
+
95
+ if (this.indexFocus === null) {
96
+ this.indexFocus = this.index;
97
+ }
98
+
99
+ switch (key) {
100
+ case keys.left:
101
+ event.preventDefault();
102
+ this.focusPreviousTab(lastTabIndex);
103
+ break;
104
+ case keys.right:
105
+ event.preventDefault();
106
+ this.focusNextTab(lastTabIndex);
107
+ break;
108
+ case keys.home:
109
+ event.preventDefault();
110
+ this.focusFirstTab();
111
+ break;
112
+ case keys.end:
113
+ event.preventDefault();
114
+ this.focusLastTab(lastTabIndex);
115
+ break;
116
+ default:
117
+ break;
118
+ }
119
+ },
120
+
121
+ onBlur() {
122
+ this.indexFocus = null;
123
+ },
124
+ },
125
+ };
126
+ </script>
@@ -0,0 +1,100 @@
1
+ <template>
2
+ <div class="TabGroup">
3
+ <slot></slot>
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ export default {
9
+ name: 'VTTabGroup',
10
+
11
+ provide() {
12
+ return {
13
+ api: () => {
14
+ const registerTab = (tab) => {
15
+ _register(this.tabs, tab);
16
+ if (tab.index === 0) tab.select();
17
+ };
18
+
19
+ const registerTabPanel = (tabPanel) => {
20
+ _register(this.tabPanels, tabPanel);
21
+ if (tabPanel.index === 0) tabPanel.show();
22
+ };
23
+
24
+ const unregisterTab = (id) => {
25
+ _unregister(this.tabs, id);
26
+ };
27
+
28
+ const unregisterTabPanel = (id) => {
29
+ _unregister(this.tabPanels, id);
30
+ };
31
+
32
+ const selectTab = (tabIndex) => {
33
+ this.tabs.forEach((tab, index) => {
34
+ index === tabIndex ? tab.select() : tab.unselect();
35
+ _handleTabPanels(tabIndex);
36
+ });
37
+ };
38
+
39
+ const focusTab = (tabIndex) => {
40
+ this.tabs[tabIndex].focus();
41
+ };
42
+
43
+ const getTabsLength = () => {
44
+ return this.tabsLength;
45
+ };
46
+
47
+ const _handleTabPanels = (tabIndex) => {
48
+ this.tabPanels.forEach((tabPanel, index) => {
49
+ index === tabIndex ? tabPanel.show() : tabPanel.hide();
50
+ });
51
+ };
52
+
53
+ const _register = (arr, item) => {
54
+ arr.push(item);
55
+ _index(arr);
56
+ };
57
+
58
+ const _unregister = (arr, id) => {
59
+ const index = _getIndex(arr, id);
60
+ arr.splice(index, 1);
61
+ };
62
+
63
+ // todo: alter index inside component
64
+ const _index = (arr) => {
65
+ arr.forEach((item, index) => {
66
+ if (!item.index) item.index = index;
67
+ });
68
+ };
69
+
70
+ const _getIndex = (arr, id) => {
71
+ return arr.findIndex((tab) => tab.id === id);
72
+ };
73
+
74
+ return {
75
+ unregisterTab,
76
+ registerTab,
77
+ unregisterTabPanel,
78
+ registerTabPanel,
79
+ selectTab,
80
+ focusTab,
81
+ getTabsLength,
82
+ };
83
+ },
84
+ };
85
+ },
86
+
87
+ data() {
88
+ return {
89
+ tabs: [],
90
+ tabPanels: [],
91
+ };
92
+ },
93
+
94
+ computed: {
95
+ tabsLength() {
96
+ return this.tabs.length;
97
+ },
98
+ },
99
+ };
100
+ </script>
@@ -0,0 +1,11 @@
1
+ <template>
2
+ <div role="tablist" class="TabList">
3
+ <slot></slot>
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ export default {
9
+ name: 'VTTabList',
10
+ };
11
+ </script>
@@ -0,0 +1,51 @@
1
+ <template>
2
+ <div
3
+ v-show="visible"
4
+ :id="id"
5
+ :aria-labelledby="`tab-${index}`"
6
+ role="tabpanel"
7
+ tabindex="0"
8
+ >
9
+ <slot></slot>
10
+ </div>
11
+ </template>
12
+
13
+ <script>
14
+ export default {
15
+ name: 'VTTabPanel',
16
+
17
+ inject: ['api'],
18
+
19
+ data() {
20
+ return {
21
+ api: this.api(),
22
+ index: null,
23
+ visible: false,
24
+ };
25
+ },
26
+
27
+ computed: {
28
+ id() {
29
+ return `tabpabel-${this.index}`;
30
+ },
31
+ },
32
+
33
+ mounted() {
34
+ this.api.registerTabPanel(this);
35
+ },
36
+
37
+ beforeDestroy() {
38
+ this.api.unregisterTabPanel(this.id);
39
+ },
40
+
41
+ methods: {
42
+ show() {
43
+ this.visible = true;
44
+ },
45
+
46
+ hide() {
47
+ this.visible = false;
48
+ },
49
+ },
50
+ };
51
+ </script>
@@ -0,0 +1,11 @@
1
+ <template>
2
+ <div class="TabPanels">
3
+ <slot></slot>
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ export default {
9
+ name: 'VTTabPanels',
10
+ };
11
+ </script>
@@ -0,0 +1,77 @@
1
+ <template>
2
+ <textarea
3
+ :class="classes"
4
+ class="form-control"
5
+ :data-theme="theme"
6
+ :value="value"
7
+ v-on="listeners"
8
+ ></textarea>
9
+ </template>
10
+
11
+ <script>
12
+ export default {
13
+ name: 'VTTextarea',
14
+
15
+ props: {
16
+ lazy: {
17
+ type: Boolean,
18
+ default: false,
19
+ },
20
+ theme: {
21
+ type: String,
22
+ default: null,
23
+ validator(value) {
24
+ return ['dark'].includes(value);
25
+ },
26
+ },
27
+ variant: {
28
+ type: [String, Object],
29
+ default: '',
30
+ validator(value) {
31
+ if (value === '' || typeof value === 'object') {
32
+ return true;
33
+ }
34
+
35
+ return ['success', 'warning', 'error'].includes(value);
36
+ },
37
+ },
38
+ value: {
39
+ type: [String, Number, Object],
40
+ default: null,
41
+ },
42
+ },
43
+
44
+ computed: {
45
+ classes() {
46
+ const classes = {};
47
+
48
+ if (this.variant) {
49
+ classes[`form-control--${this.variant}`] = true;
50
+ }
51
+
52
+ return classes;
53
+ },
54
+
55
+ listeners() {
56
+ // `Object.assign` merges objects together to form a new object
57
+ return Object.assign(
58
+ {},
59
+ // We add all the listeners from the parent
60
+ this.$listeners,
61
+ // Then we can add custom listeners or override the
62
+ // behavior of some listeners.
63
+ {
64
+ // This ensures that the component works with v-model
65
+ input: (event) => {
66
+ if (this.lazy) return;
67
+ this.$emit('input', event.target.value);
68
+ },
69
+ blur: (event) => {
70
+ this.$emit('blur', event);
71
+ },
72
+ }
73
+ );
74
+ },
75
+ },
76
+ };
77
+ </script>