@uninspired/cookie-banner 0.0.12 → 0.0.14
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/README.md +206 -0
- package/dist/react/index.cjs +93 -46
- package/dist/react/index.d.ts +12 -16
- package/dist/react/index.js +95 -48
- package/dist/script/index.cjs +16 -16
- package/dist/script/index.d.ts +11 -14
- package/dist/script/index.js +1232 -1183
- package/package.json +1 -1
package/dist/react/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import require$$0, {
|
|
1
|
+
import require$$0, { useState, useEffect, useMemo, useCallback, createContext, useContext } from "react";
|
|
2
2
|
import { Item, Trigger, Content, Root as Root$3 } from "@radix-ui/react-accordion";
|
|
3
3
|
import { ChevronDown, ChevronUp } from "lucide-react";
|
|
4
|
-
import { Root as Root$1, Content as Content$1, Title, Description, Close } from "@radix-ui/react-dialog";
|
|
4
|
+
import { Root as Root$1, Portal, Content as Content$1, Title, Description, Close } from "@radix-ui/react-dialog";
|
|
5
5
|
import { Root as Root$2, Trigger as Trigger$1, Content as Content$2 } from "@radix-ui/react-collapsible";
|
|
6
6
|
import { Root, Thumb } from "@radix-ui/react-switch";
|
|
7
7
|
var jsxRuntime = { exports: {} };
|
|
@@ -1949,6 +1949,7 @@ function optional(innerType) {
|
|
|
1949
1949
|
});
|
|
1950
1950
|
}
|
|
1951
1951
|
const remoteScriptDefinitionSchema = /* @__PURE__ */ object({
|
|
1952
|
+
id: /* @__PURE__ */ string(),
|
|
1952
1953
|
variant: /* @__PURE__ */ literal("remote"),
|
|
1953
1954
|
type: /* @__PURE__ */ optional(/* @__PURE__ */ string()),
|
|
1954
1955
|
src: /* @__PURE__ */ string(),
|
|
@@ -1958,6 +1959,7 @@ const remoteScriptDefinitionSchema = /* @__PURE__ */ object({
|
|
|
1958
1959
|
integrity: /* @__PURE__ */ optional(/* @__PURE__ */ string())
|
|
1959
1960
|
});
|
|
1960
1961
|
const inlineScriptDefinitionSchema = /* @__PURE__ */ object({
|
|
1962
|
+
id: /* @__PURE__ */ string(),
|
|
1961
1963
|
variant: /* @__PURE__ */ literal("inline"),
|
|
1962
1964
|
type: /* @__PURE__ */ optional(/* @__PURE__ */ string()),
|
|
1963
1965
|
async: /* @__PURE__ */ optional(/* @__PURE__ */ boolean()),
|
|
@@ -1982,14 +1984,59 @@ const Switch = ({ label: label2, id, ...rest }) => {
|
|
|
1982
1984
|
] });
|
|
1983
1985
|
};
|
|
1984
1986
|
const selectionSchema = /* @__PURE__ */ record(/* @__PURE__ */ string(), /* @__PURE__ */ boolean());
|
|
1987
|
+
class Cookies {
|
|
1988
|
+
#domain;
|
|
1989
|
+
#prefix;
|
|
1990
|
+
#sameSite;
|
|
1991
|
+
#secure;
|
|
1992
|
+
constructor(options) {
|
|
1993
|
+
this.#domain = options.domain;
|
|
1994
|
+
this.#prefix = options.prefix ?? "us-cookie-banner-";
|
|
1995
|
+
this.#sameSite = options.sameSite ?? "lax";
|
|
1996
|
+
this.#secure = options.secure ?? true;
|
|
1997
|
+
}
|
|
1998
|
+
#serialize = (data) => {
|
|
1999
|
+
return btoa(String.fromCharCode(...new TextEncoder().encode(data)));
|
|
2000
|
+
};
|
|
2001
|
+
#deserialize = (data) => {
|
|
2002
|
+
return new TextDecoder().decode(
|
|
2003
|
+
Uint8Array.from(atob(data), (c) => c.charCodeAt(0))
|
|
2004
|
+
);
|
|
2005
|
+
};
|
|
2006
|
+
set = (key, value, ttl = 365) => {
|
|
2007
|
+
const expires = new Date(Date.now() + ttl * 24 * 60 * 60 * 1e3);
|
|
2008
|
+
const cookie = `${this.#prefix}${key}=${this.#serialize(value)}; expires=${expires}; path=/; domain=${this.#domain}; SameSite=${this.#sameSite}; ${this.#secure ? "secure" : ""}`;
|
|
2009
|
+
document.cookie = cookie;
|
|
2010
|
+
};
|
|
2011
|
+
get = (key) => {
|
|
2012
|
+
const match = document.cookie.match(
|
|
2013
|
+
new RegExp(`(?:^|; )${this.#prefix}${key}=([^;]*)`)
|
|
2014
|
+
);
|
|
2015
|
+
if (!match) return null;
|
|
2016
|
+
const [_, serializedValue] = match;
|
|
2017
|
+
if (!serializedValue) return null;
|
|
2018
|
+
return this.#deserialize(serializedValue);
|
|
2019
|
+
};
|
|
2020
|
+
remove = (key) => {
|
|
2021
|
+
this.set(key, "", -1);
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
const SELECTION_KEY = "selection";
|
|
1985
2025
|
const SelectionContext = createContext(null);
|
|
1986
2026
|
const SelectionProvider = ({
|
|
1987
2027
|
children,
|
|
1988
2028
|
items,
|
|
1989
|
-
|
|
2029
|
+
cookie
|
|
1990
2030
|
}) => {
|
|
1991
|
-
|
|
1992
|
-
|
|
2031
|
+
const { domain, prefix, sameSite, secure } = cookie;
|
|
2032
|
+
const [cookies, setCookies] = useState(
|
|
2033
|
+
new Cookies({ domain, prefix, sameSite, secure })
|
|
2034
|
+
);
|
|
2035
|
+
useEffect(() => {
|
|
2036
|
+
setCookies(new Cookies({ domain, prefix, sameSite, secure }));
|
|
2037
|
+
}, [domain, prefix, sameSite, secure]);
|
|
2038
|
+
function getCookieSelection() {
|
|
2039
|
+
const storedSelection = cookies.get(SELECTION_KEY);
|
|
1993
2040
|
if (!storedSelection) return null;
|
|
1994
2041
|
const { success, data } = selectionSchema.safeParse(
|
|
1995
2042
|
JSON.parse(storedSelection)
|
|
@@ -2004,10 +2051,10 @@ const SelectionProvider = ({
|
|
|
2004
2051
|
[items]
|
|
2005
2052
|
);
|
|
2006
2053
|
const [selection, setSelection] = useState(
|
|
2007
|
-
|
|
2054
|
+
getCookieSelection() ?? defaultSelection
|
|
2008
2055
|
);
|
|
2009
2056
|
const [savedSelection, setSavedSelection] = useState(
|
|
2010
|
-
|
|
2057
|
+
getCookieSelection()
|
|
2011
2058
|
);
|
|
2012
2059
|
function toggleSelection(key, value) {
|
|
2013
2060
|
const newSelection = selection ? { ...selection, [key]: value } : { [key]: value };
|
|
@@ -2022,47 +2069,51 @@ const SelectionProvider = ({
|
|
|
2022
2069
|
(item) => selectedValues.includes(item.value)
|
|
2023
2070
|
);
|
|
2024
2071
|
for (const item of items) {
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2072
|
+
if (!item.scripts) continue;
|
|
2073
|
+
for (const script of item.scripts) {
|
|
2074
|
+
const elem = document.head.querySelector(`#${item.value}-${script.id}`);
|
|
2075
|
+
if (!elem) continue;
|
|
2076
|
+
elem.remove();
|
|
2077
|
+
}
|
|
2028
2078
|
}
|
|
2029
2079
|
for (const item of mountedItems) {
|
|
2030
|
-
|
|
2031
|
-
elem.id = item.value;
|
|
2032
|
-
if (!item.script) {
|
|
2080
|
+
if (!item.scripts) {
|
|
2033
2081
|
if (!item.required)
|
|
2034
2082
|
console.warn("CookieBanner: Missing script for", item.value);
|
|
2035
2083
|
continue;
|
|
2036
2084
|
}
|
|
2037
|
-
const
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2085
|
+
for (const script of item.scripts) {
|
|
2086
|
+
if (!script) continue;
|
|
2087
|
+
const elem = document.createElement("script");
|
|
2088
|
+
elem.id = `${item.value}-${script.id}`;
|
|
2089
|
+
const { success, data } = scriptDefinitionSchema.safeParse(script);
|
|
2090
|
+
if (!success) {
|
|
2091
|
+
console.error(
|
|
2092
|
+
"CookieBanner: Invalid script definition for",
|
|
2093
|
+
item.value
|
|
2094
|
+
);
|
|
2095
|
+
continue;
|
|
2096
|
+
}
|
|
2097
|
+
elem.type = data.type ?? "text/javascript";
|
|
2098
|
+
elem.async = data.async ?? false;
|
|
2099
|
+
elem.defer = data.defer ?? false;
|
|
2100
|
+
switch (data.variant) {
|
|
2101
|
+
case "inline":
|
|
2102
|
+
elem.textContent = data.content;
|
|
2103
|
+
break;
|
|
2104
|
+
case "remote":
|
|
2105
|
+
elem.src = data.src;
|
|
2106
|
+
elem.crossOrigin = data.crossorigin ?? null;
|
|
2107
|
+
elem.integrity = data.integrity ?? "";
|
|
2108
|
+
break;
|
|
2109
|
+
}
|
|
2110
|
+
document.head.appendChild(elem);
|
|
2046
2111
|
}
|
|
2047
|
-
elem.type = script.type ?? "text/javascript";
|
|
2048
|
-
elem.async = script.async ?? false;
|
|
2049
|
-
elem.defer = script.defer ?? false;
|
|
2050
|
-
switch (script.variant) {
|
|
2051
|
-
case "inline":
|
|
2052
|
-
elem.textContent = script.content;
|
|
2053
|
-
break;
|
|
2054
|
-
case "remote":
|
|
2055
|
-
elem.src = script.src;
|
|
2056
|
-
elem.crossOrigin = script.crossorigin ?? null;
|
|
2057
|
-
elem.integrity = script.integrity ?? "";
|
|
2058
|
-
break;
|
|
2059
|
-
}
|
|
2060
|
-
document.head.appendChild(elem);
|
|
2061
2112
|
}
|
|
2062
2113
|
}, [savedSelection, items]);
|
|
2063
2114
|
function saveSelection(newSelection) {
|
|
2064
2115
|
setSavedSelection(newSelection);
|
|
2065
|
-
|
|
2116
|
+
cookies.set(SELECTION_KEY, JSON.stringify(newSelection));
|
|
2066
2117
|
}
|
|
2067
2118
|
const onDeclineAll = useCallback(() => {
|
|
2068
2119
|
const newSelection = Object.fromEntries(
|
|
@@ -2165,7 +2216,7 @@ const BannerItem = ({
|
|
|
2165
2216
|
] });
|
|
2166
2217
|
};
|
|
2167
2218
|
const BannerContent = ({
|
|
2168
|
-
|
|
2219
|
+
container,
|
|
2169
2220
|
heading: heading2 = "We use cookies.",
|
|
2170
2221
|
subheading = "Please define your selection below.",
|
|
2171
2222
|
selectLabel = "Select",
|
|
@@ -2179,12 +2230,12 @@ const BannerContent = ({
|
|
|
2179
2230
|
const { onSave, onDeclineAll, selectionTaken } = useSelection();
|
|
2180
2231
|
const [openItem, setOpenItem] = useState(void 0);
|
|
2181
2232
|
const [settingsOpen, setSettingsOpen] = useState(defaultSettingsOpen);
|
|
2182
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(Root$1, { open: !selectionTaken, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
2233
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(Root$1, { open: !selectionTaken, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Portal, { container, children: /* @__PURE__ */ jsxRuntimeExports.jsx(
|
|
2183
2234
|
Content$1,
|
|
2184
2235
|
{
|
|
2185
2236
|
className: clx({
|
|
2186
2237
|
[classes$2.root]: true,
|
|
2187
|
-
[classes$2.noTarget]:
|
|
2238
|
+
[classes$2.noTarget]: !container
|
|
2188
2239
|
}),
|
|
2189
2240
|
children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
|
|
2190
2241
|
Root$2,
|
|
@@ -2242,23 +2293,19 @@ const BannerContent = ({
|
|
|
2242
2293
|
}
|
|
2243
2294
|
)
|
|
2244
2295
|
}
|
|
2245
|
-
) });
|
|
2296
|
+
) }) });
|
|
2246
2297
|
};
|
|
2247
|
-
const Banner = ({
|
|
2248
|
-
localStorageKey = "cb-selection",
|
|
2249
|
-
items,
|
|
2250
|
-
...rest
|
|
2251
|
-
}) => {
|
|
2298
|
+
const Banner = ({ cookie, items, ...rest }) => {
|
|
2252
2299
|
const selectionItems = useMemo(
|
|
2253
2300
|
() => items.filter((item) => !item.required).map((item) => ({
|
|
2254
2301
|
value: item.value,
|
|
2255
|
-
|
|
2302
|
+
scripts: item.scripts,
|
|
2256
2303
|
defaultSelected: item.defaultSelected,
|
|
2257
2304
|
required: item.required
|
|
2258
2305
|
})),
|
|
2259
2306
|
[items]
|
|
2260
2307
|
);
|
|
2261
|
-
return /* @__PURE__ */ jsxRuntimeExports.jsx(SelectionProvider, { items: selectionItems,
|
|
2308
|
+
return /* @__PURE__ */ jsxRuntimeExports.jsx(SelectionProvider, { items: selectionItems, cookie, children: /* @__PURE__ */ jsxRuntimeExports.jsx(BannerContent, { items, cookie, ...rest }) });
|
|
2262
2309
|
};
|
|
2263
2310
|
export {
|
|
2264
2311
|
Banner
|