@el7ven/cookie-kit 0.2.17 → 0.2.19
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
|
@@ -24,42 +24,33 @@
|
|
|
24
24
|
|
|
25
25
|
<script setup>
|
|
26
26
|
import { computed, nextTick, onUnmounted, ref, watch } from 'vue'
|
|
27
|
-
import {
|
|
27
|
+
import { useCookieKitVue, DEFAULT_CONFIG } from './index.js'
|
|
28
28
|
import CookieDrawer from './CookieDrawer.vue'
|
|
29
29
|
|
|
30
30
|
// Props for customization
|
|
31
31
|
const props = defineProps({
|
|
32
32
|
config: {
|
|
33
33
|
type: Object,
|
|
34
|
-
default: () =>
|
|
35
|
-
categories: {
|
|
36
|
-
necessary: { required: true, enabled: true, name: 'Essential Cookies' },
|
|
37
|
-
analytics: { required: false, enabled: true, name: 'Analytics Cookies' },
|
|
38
|
-
marketing: { required: false, enabled: false, name: 'Marketing Cookies' }
|
|
39
|
-
},
|
|
40
|
-
consentExpireDays: 365,
|
|
41
|
-
version: '0.2.0',
|
|
42
|
-
theme: 'light'
|
|
43
|
-
})
|
|
34
|
+
default: () => DEFAULT_CONFIG
|
|
44
35
|
}
|
|
45
36
|
})
|
|
46
37
|
|
|
47
|
-
// Use the
|
|
38
|
+
// Use the Vue composable
|
|
48
39
|
const {
|
|
49
40
|
consent,
|
|
50
41
|
acceptAll,
|
|
51
42
|
rejectAll,
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
acceptSelected,
|
|
44
|
+
hasConsented,
|
|
54
45
|
hasCategoryConsent
|
|
55
|
-
} =
|
|
46
|
+
} = useCookieKitVue(props.config)
|
|
56
47
|
|
|
57
48
|
// Local state
|
|
58
|
-
const isVisible = computed(() => !
|
|
49
|
+
const isVisible = computed(() => !hasConsented.value)
|
|
59
50
|
const isSettingsMode = ref(false)
|
|
60
51
|
const currentTab = ref('privacy')
|
|
61
|
-
const categories =
|
|
62
|
-
const consentVersion = computed(() => props.config.version || '0.
|
|
52
|
+
const categories = computed(() => consent.value?.categories || props.config.categories)
|
|
53
|
+
const consentVersion = computed(() => props.config.version || '1.0.0')
|
|
63
54
|
const capabilities = computed(() => ({}))
|
|
64
55
|
const isV2 = computed(() => true)
|
|
65
56
|
const theme = computed(() => props.config.theme || 'light')
|
|
@@ -89,13 +80,11 @@ const closeSettings = () => {
|
|
|
89
80
|
}
|
|
90
81
|
|
|
91
82
|
const acceptSelection = () => {
|
|
92
|
-
//
|
|
93
|
-
Object.keys(categories.value)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
})
|
|
83
|
+
// Get selected category IDs
|
|
84
|
+
const selectedIds = Object.keys(categories.value)
|
|
85
|
+
.filter(id => categories.value[id]?.enabled)
|
|
86
|
+
|
|
87
|
+
acceptSelected(selectedIds)
|
|
99
88
|
}
|
|
100
89
|
|
|
101
90
|
const selectTab = (tabId) => {
|
|
@@ -4,9 +4,10 @@ export function useCookieConsent(config = {}) {
|
|
|
4
4
|
const isVisible = ref(false)
|
|
5
5
|
const isSettingsMode = ref(false)
|
|
6
6
|
const currentTab = ref('categories')
|
|
7
|
-
const consentVersion = ref('1.0.0')
|
|
7
|
+
const consentVersion = ref(config.consentVersion || '1.0.0')
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
// Use config categories or default
|
|
10
|
+
const categories = ref(config.categories || {
|
|
10
11
|
necessary: {
|
|
11
12
|
required: true,
|
|
12
13
|
enabled: true,
|
|
@@ -35,17 +36,19 @@ export function useCookieConsent(config = {}) {
|
|
|
35
36
|
canCustomize: true
|
|
36
37
|
})
|
|
37
38
|
|
|
38
|
-
const isV2 = ref(
|
|
39
|
+
const isV2 = ref(consentVersion.value === 'v2')
|
|
39
40
|
|
|
40
41
|
const hasConsent = computed(() => {
|
|
41
42
|
return localStorage.getItem('cookie-consent') !== null
|
|
42
43
|
})
|
|
43
44
|
|
|
44
|
-
const
|
|
45
|
+
const consent = computed(() => {
|
|
45
46
|
const stored = localStorage.getItem('cookie-consent')
|
|
46
47
|
return stored ? JSON.parse(stored) : null
|
|
47
48
|
})
|
|
48
49
|
|
|
50
|
+
const consentData = computed(() => consent.value)
|
|
51
|
+
|
|
49
52
|
const showConsent = () => {
|
|
50
53
|
isVisible.value = true
|
|
51
54
|
}
|
|
@@ -54,6 +57,10 @@ export function useCookieConsent(config = {}) {
|
|
|
54
57
|
isVisible.value = false
|
|
55
58
|
}
|
|
56
59
|
|
|
60
|
+
const showSettings = () => {
|
|
61
|
+
isSettingsMode.value = true
|
|
62
|
+
}
|
|
63
|
+
|
|
57
64
|
const openSettings = () => {
|
|
58
65
|
isSettingsMode.value = true
|
|
59
66
|
}
|
|
@@ -62,10 +69,19 @@ export function useCookieConsent(config = {}) {
|
|
|
62
69
|
isSettingsMode.value = false
|
|
63
70
|
}
|
|
64
71
|
|
|
72
|
+
const close = () => {
|
|
73
|
+
isVisible.value = false
|
|
74
|
+
isSettingsMode.value = false
|
|
75
|
+
}
|
|
76
|
+
|
|
65
77
|
const selectTab = (tab) => {
|
|
66
78
|
currentTab.value = tab
|
|
67
79
|
}
|
|
68
80
|
|
|
81
|
+
const setTab = (tab) => {
|
|
82
|
+
currentTab.value = tab
|
|
83
|
+
}
|
|
84
|
+
|
|
69
85
|
const toggleCategory = (categoryName, enabled) => {
|
|
70
86
|
if (categories.value[categoryName] && !categories.value[categoryName].required) {
|
|
71
87
|
categories.value[categoryName].enabled = enabled
|
|
@@ -73,7 +89,7 @@ export function useCookieConsent(config = {}) {
|
|
|
73
89
|
}
|
|
74
90
|
|
|
75
91
|
const acceptSelection = () => {
|
|
76
|
-
const
|
|
92
|
+
const consentRecord = {
|
|
77
93
|
version: consentVersion.value,
|
|
78
94
|
timestamp: new Date().toISOString(),
|
|
79
95
|
categories: Object.keys(categories.value).reduce((acc, key) => {
|
|
@@ -82,7 +98,7 @@ export function useCookieConsent(config = {}) {
|
|
|
82
98
|
}, {})
|
|
83
99
|
}
|
|
84
100
|
|
|
85
|
-
localStorage.setItem('cookie-consent', JSON.stringify(
|
|
101
|
+
localStorage.setItem('cookie-consent', JSON.stringify(consentRecord))
|
|
86
102
|
capabilities.value.hasConsented = true
|
|
87
103
|
hideConsent()
|
|
88
104
|
closeSettings()
|
|
@@ -104,6 +120,10 @@ export function useCookieConsent(config = {}) {
|
|
|
104
120
|
acceptSelection()
|
|
105
121
|
}
|
|
106
122
|
|
|
123
|
+
const acceptSelected = () => {
|
|
124
|
+
acceptSelection()
|
|
125
|
+
}
|
|
126
|
+
|
|
107
127
|
const resetConsent = () => {
|
|
108
128
|
localStorage.removeItem('cookie-consent')
|
|
109
129
|
capabilities.value.hasConsented = false
|
|
@@ -114,6 +134,33 @@ export function useCookieConsent(config = {}) {
|
|
|
114
134
|
})
|
|
115
135
|
}
|
|
116
136
|
|
|
137
|
+
// Focus trap functions
|
|
138
|
+
let activeElement = null
|
|
139
|
+
|
|
140
|
+
const activateFocusTrap = (element) => {
|
|
141
|
+
if (!element) return
|
|
142
|
+
|
|
143
|
+
activeElement = document.activeElement
|
|
144
|
+
const focusableElements = element.querySelectorAll(
|
|
145
|
+
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
if (focusableElements.length > 0) {
|
|
149
|
+
focusableElements[0].focus()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const deactivateFocusTrap = () => {
|
|
154
|
+
if (activeElement && activeElement.focus) {
|
|
155
|
+
activeElement.focus()
|
|
156
|
+
}
|
|
157
|
+
activeElement = null
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const cleanup = () => {
|
|
161
|
+
deactivateFocusTrap()
|
|
162
|
+
}
|
|
163
|
+
|
|
117
164
|
const handleClose = () => {
|
|
118
165
|
hideConsent()
|
|
119
166
|
closeSettings()
|
|
@@ -151,6 +198,10 @@ export function useCookieConsent(config = {}) {
|
|
|
151
198
|
}
|
|
152
199
|
})
|
|
153
200
|
|
|
201
|
+
onUnmounted(() => {
|
|
202
|
+
cleanup()
|
|
203
|
+
})
|
|
204
|
+
|
|
154
205
|
return {
|
|
155
206
|
isVisible,
|
|
156
207
|
isSettingsMode,
|
|
@@ -160,17 +211,25 @@ export function useCookieConsent(config = {}) {
|
|
|
160
211
|
isV2,
|
|
161
212
|
consentVersion,
|
|
162
213
|
hasConsent,
|
|
214
|
+
consent,
|
|
163
215
|
consentData,
|
|
164
216
|
showConsent,
|
|
165
217
|
hideConsent,
|
|
218
|
+
showSettings,
|
|
166
219
|
openSettings,
|
|
167
220
|
closeSettings,
|
|
221
|
+
close,
|
|
168
222
|
selectTab,
|
|
223
|
+
setTab,
|
|
169
224
|
toggleCategory,
|
|
170
225
|
acceptSelection,
|
|
171
226
|
acceptAll,
|
|
172
227
|
rejectAll,
|
|
228
|
+
acceptSelected,
|
|
173
229
|
resetConsent,
|
|
230
|
+
activateFocusTrap,
|
|
231
|
+
deactivateFocusTrap,
|
|
232
|
+
cleanup,
|
|
174
233
|
handleClose,
|
|
175
234
|
handleOpenSettings,
|
|
176
235
|
handleSelectTab,
|