@reykjavik/webtools 0.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/CHANGELOG.md +11 -0
- package/CookieHubConsent.d.ts +231 -0
- package/CookieHubConsent.js +127 -0
- package/README.md +452 -0
- package/esm/CookieHubConsent.d.ts +231 -0
- package/esm/CookieHubConsent.js +99 -0
- package/esm/http.d.ts +82 -0
- package/esm/http.js +125 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.js +1 -0
- package/esm/next/SiteImprove.d.ts +81 -0
- package/esm/next/SiteImprove.js +100 -0
- package/esm/next/http.d.ts +56 -0
- package/esm/next/http.js +81 -0
- package/esm/package.json +1 -0
- package/http.d.ts +82 -0
- package/http.js +129 -0
- package/index.d.ts +6 -0
- package/index.js +2 -0
- package/next/SiteImprove.d.ts +81 -0
- package/next/SiteImprove.js +131 -0
- package/next/http.d.ts +56 -0
- package/next/http.js +103 -0
- package/package.json +47 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { EitherObj } from '@reykjavik/hanna-utils';
|
|
3
|
+
declare global {
|
|
4
|
+
interface Window {
|
|
5
|
+
cookiehub: CookieHub;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
type CookieHub = {
|
|
9
|
+
/**
|
|
10
|
+
* Used to initialize, and configure, CookieHub.
|
|
11
|
+
*
|
|
12
|
+
* (Thin wrapper for `window.cookiehub.initialize`)
|
|
13
|
+
*
|
|
14
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
15
|
+
*/
|
|
16
|
+
load(options?: CookieHubOptions): void;
|
|
17
|
+
/**
|
|
18
|
+
* Used to initialize, and configure, CookieHub.
|
|
19
|
+
*
|
|
20
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
21
|
+
*/
|
|
22
|
+
initialize(options?: CookieHubOptions): void;
|
|
23
|
+
/**
|
|
24
|
+
* Used to check if the user has already made cookie choices.
|
|
25
|
+
*
|
|
26
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
27
|
+
*/
|
|
28
|
+
hasAnswered(): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Used to check if the user has allowed the cookie category specified in
|
|
31
|
+
* the category parameter.
|
|
32
|
+
*
|
|
33
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
34
|
+
*/
|
|
35
|
+
hasConsented(category: CookieHubCategory): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Used to check if the CookieHub script has been initialized
|
|
38
|
+
*
|
|
39
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
40
|
+
*/
|
|
41
|
+
hasInitialized(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Open the CookieHub dialog.
|
|
44
|
+
*
|
|
45
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
46
|
+
*/
|
|
47
|
+
openDialog(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Close the CookieHub dialog if it's still open.
|
|
50
|
+
*
|
|
51
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
52
|
+
*/
|
|
53
|
+
closeDialog(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Open the CookieHub settings dialog. This may be useful if you choose to
|
|
56
|
+
* hide the settings icon which is usually used to open the CookieHub
|
|
57
|
+
* settings dialog and want to create a custom link or button to open the
|
|
58
|
+
* dialog instead.
|
|
59
|
+
*
|
|
60
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
61
|
+
*/
|
|
62
|
+
openSettings(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Close the CookieHub settings dialog.
|
|
65
|
+
*
|
|
66
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
67
|
+
*/
|
|
68
|
+
closeSettings(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Denys all cookie categories.
|
|
71
|
+
*
|
|
72
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
73
|
+
*/
|
|
74
|
+
denyAll(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Allows all cookie categories.
|
|
77
|
+
*
|
|
78
|
+
* @see https://support.cookiehub.com/article/86-methods-functions
|
|
79
|
+
*/
|
|
80
|
+
allowAll(): void;
|
|
81
|
+
};
|
|
82
|
+
type CookieHubCategory = 'necessary' | 'preferences' | 'analytics' | 'marketing' | 'uncategorized';
|
|
83
|
+
type CookieHubOptions = {
|
|
84
|
+
/**
|
|
85
|
+
* Fired when CookieHub has finished loading.
|
|
86
|
+
*
|
|
87
|
+
* @see https://support.cookiehub.com/article/87-events
|
|
88
|
+
*/
|
|
89
|
+
onInitialise?: (this: CookieHub, status: CookiehubState) => void;
|
|
90
|
+
/**
|
|
91
|
+
* Fired any time users make changes to cookie choices and click the
|
|
92
|
+
* Save Settings or if the Allow All Cookies button is clicked.
|
|
93
|
+
*
|
|
94
|
+
* @see https://support.cookiehub.com/article/87-events
|
|
95
|
+
*/
|
|
96
|
+
onStatusChange?: (this: CookieHub, status: CookiehubState, previousStatus: CookiehubState) => void;
|
|
97
|
+
/**
|
|
98
|
+
* Fired any time a cookie category is allowed that was previously disallowed.
|
|
99
|
+
*
|
|
100
|
+
* @see https://support.cookiehub.com/article/87-events
|
|
101
|
+
*/
|
|
102
|
+
onAllow?: (this: CookieHub, category: CookieHubCategory) => void;
|
|
103
|
+
/**
|
|
104
|
+
* Fired any time a cookie category consent is revoked for a category that
|
|
105
|
+
* was previously allowed.
|
|
106
|
+
*
|
|
107
|
+
* @see https://support.cookiehub.com/article/87-events
|
|
108
|
+
*/
|
|
109
|
+
onRevoke?: (this: CookieHub, category: CookieHubCategory) => void;
|
|
110
|
+
dialog?: {
|
|
111
|
+
/**
|
|
112
|
+
* Controls the display of the action buttons in the popup dialog.
|
|
113
|
+
*
|
|
114
|
+
* @see https://support.cookiehub.com/article/128-changing-the-order-of-action-buttons
|
|
115
|
+
*/
|
|
116
|
+
actions?: Array<'allow' | 'deny' | 'settings'>;
|
|
117
|
+
};
|
|
118
|
+
cookie?: {
|
|
119
|
+
/**
|
|
120
|
+
* Empty string (`''`) will instruct the browser to set the cookie on
|
|
121
|
+
* the current domain instead of the top level domain which is the
|
|
122
|
+
* default value.
|
|
123
|
+
*
|
|
124
|
+
* @see https://support.cookiehub.com/article/85-cookiehub-on-multiple-domains-hosts
|
|
125
|
+
*/
|
|
126
|
+
domain?: string;
|
|
127
|
+
/**
|
|
128
|
+
* Controls the cookie behavior and access for the cookiehub cookie
|
|
129
|
+
*
|
|
130
|
+
* Possible values are:
|
|
131
|
+
*
|
|
132
|
+
* - **Lax** (Default): Allows pages on the domain and third party links
|
|
133
|
+
* to access the cookie. This is the default setting for CookieHub.
|
|
134
|
+
* - **Strict**: Only allows pages on the domain that set the cookie to
|
|
135
|
+
* access it. Links from third parties won’t be able to access the
|
|
136
|
+
* cookie.
|
|
137
|
+
* - **None**: No domain limitations and third-party cookies can fire.
|
|
138
|
+
*
|
|
139
|
+
* @see https://support.cookiehub.com/article/84-cookie-security-and-the-samesite-attribute
|
|
140
|
+
*/
|
|
141
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
142
|
+
/**
|
|
143
|
+
* If the Secure attribute is set to true, the cookie will only be accessible
|
|
144
|
+
* if the page being loaded is loaded over a secure connection.
|
|
145
|
+
*
|
|
146
|
+
* Defaults to `false` but it's recommended to set it to true
|
|
147
|
+
*
|
|
148
|
+
* @see https://support.cookiehub.com/article/84-cookie-security-and-the-samesite-attribute
|
|
149
|
+
*/
|
|
150
|
+
secure?: boolean;
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
type CookiehubState = {
|
|
154
|
+
answered: boolean;
|
|
155
|
+
preconsent: boolean;
|
|
156
|
+
revision: number;
|
|
157
|
+
dnt: boolean;
|
|
158
|
+
allowSale: boolean;
|
|
159
|
+
implict: boolean;
|
|
160
|
+
region: string;
|
|
161
|
+
token: string;
|
|
162
|
+
timestamp: Date;
|
|
163
|
+
categories: Array<CookieHubCategoryState>;
|
|
164
|
+
};
|
|
165
|
+
type CookieHubCategoryState = {
|
|
166
|
+
cid: number;
|
|
167
|
+
id: CookieHubCategory;
|
|
168
|
+
value: boolean;
|
|
169
|
+
preconsent: boolean;
|
|
170
|
+
fired: boolean;
|
|
171
|
+
};
|
|
172
|
+
type CookieHubContextState = {
|
|
173
|
+
consent: Record<Exclude<CookieHubCategory, 'necessary'>, boolean>;
|
|
174
|
+
};
|
|
175
|
+
export type CookieHubProviderProps = EitherObj<{
|
|
176
|
+
/**
|
|
177
|
+
* Yuour CookieHub account ID.
|
|
178
|
+
*
|
|
179
|
+
* It's a random-looking alpha-numerical string, and it can usually be
|
|
180
|
+
* extracted from the script embed URL like this:
|
|
181
|
+
* `"https://cookiehub.net/c2/[ACCOUNT_ID].js"`
|
|
182
|
+
*
|
|
183
|
+
* @see https://support.cookiehub.com/article/155-manual-implementation-guide
|
|
184
|
+
*/
|
|
185
|
+
accountId: string;
|
|
186
|
+
}, {
|
|
187
|
+
/**
|
|
188
|
+
* The full CookieHub embed script URL.
|
|
189
|
+
*
|
|
190
|
+
* Something like `"https://cookiehub.net/c2/[ACCOUNT_ID].js"`
|
|
191
|
+
*
|
|
192
|
+
* @see https://support.cookiehub.com/article/155-manual-implementation-guide
|
|
193
|
+
*/
|
|
194
|
+
scriptUrl: string;
|
|
195
|
+
}> & {
|
|
196
|
+
children: ReactNode;
|
|
197
|
+
/**
|
|
198
|
+
* Custom callback that fires when CookieHub has initialized.
|
|
199
|
+
*
|
|
200
|
+
* To subscribe to other events run:
|
|
201
|
+
*
|
|
202
|
+
* ```js
|
|
203
|
+
* window.cookiehub.initialize({
|
|
204
|
+
* // Event handlers other than `onInitialize`
|
|
205
|
+
* })
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @see https://support.cookiehub.com/article/87-events
|
|
209
|
+
*/
|
|
210
|
+
options?: CookieHubOptions;
|
|
211
|
+
/**
|
|
212
|
+
* Error callback for if the CookieHub script fails to load.
|
|
213
|
+
*/
|
|
214
|
+
onError?: OnErrorEventHandlerNonNull;
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* This context provider component loads and initialises the CookieHub consent
|
|
218
|
+
* management script and sets up a React state object with the relevant user
|
|
219
|
+
* consent flags.
|
|
220
|
+
*
|
|
221
|
+
* @see https://github.com/reykjavikcity/webtools/tree/v0.1##cookiehubprovider-component
|
|
222
|
+
*/
|
|
223
|
+
export declare const CookieHubProvider: (props: CookieHubProviderProps) => JSX.Element;
|
|
224
|
+
/**
|
|
225
|
+
* Returns up-to-date cookie consent flags. For use in React components or hook
|
|
226
|
+
* functions.
|
|
227
|
+
*
|
|
228
|
+
* @see https://github.com/reykjavikcity/webtools/tree/v0.1##usecookiehubconsent
|
|
229
|
+
*/
|
|
230
|
+
export declare const useCookieHubConsent: () => Partial<CookieHubContextState['consent']>;
|
|
231
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.useCookieHubConsent = exports.CookieHubProvider = void 0;
|
|
27
|
+
const react_1 = __importStar(require("react"));
|
|
28
|
+
// END: Mock typing of CookieHub's script API
|
|
29
|
+
// --------------------------------------------------------------------------
|
|
30
|
+
//
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
const idToken = '[ACCOUNT_ID]';
|
|
33
|
+
const scriptUrlTemplate = process.env.NODE_ENV === 'production'
|
|
34
|
+
? `https://cookiehub.net/c2/${idToken}.js`
|
|
35
|
+
: `https://cookiehub.net/dev/${idToken}.js`;
|
|
36
|
+
const CookieHubContext = (0, react_1.createContext)(undefined);
|
|
37
|
+
/**
|
|
38
|
+
* Used as the initial/default state when CookieHubProvider mounts and
|
|
39
|
+
* prepares to load the script
|
|
40
|
+
*/
|
|
41
|
+
const initialConsentState = {
|
|
42
|
+
consent: {
|
|
43
|
+
analytics: false,
|
|
44
|
+
preferences: false,
|
|
45
|
+
marketing: false,
|
|
46
|
+
uncategorized: false,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Moves the CookieHub `<div/>` to the bottom of the dom tree
|
|
51
|
+
* for better accessability in screen readers.
|
|
52
|
+
*/
|
|
53
|
+
const moveCookiehubScriptInDomTree = () => {
|
|
54
|
+
const cookieHubPromptElm = document.querySelector('.ch2');
|
|
55
|
+
if (cookieHubPromptElm) {
|
|
56
|
+
document.body.append(cookieHubPromptElm);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* This context provider component loads and initialises the CookieHub consent
|
|
61
|
+
* management script and sets up a React state object with the relevant user
|
|
62
|
+
* consent flags.
|
|
63
|
+
*
|
|
64
|
+
* @see https://github.com/reykjavikcity/webtools/tree/v0.1##cookiehubprovider-component
|
|
65
|
+
*/
|
|
66
|
+
const CookieHubProvider = (props) => {
|
|
67
|
+
const [state, setState] = (0, react_1.useState)(initialConsentState);
|
|
68
|
+
(0, react_1.useEffect)(() => {
|
|
69
|
+
const script = document.createElement('script');
|
|
70
|
+
script.async = true;
|
|
71
|
+
const opts = props.options || {};
|
|
72
|
+
script.src =
|
|
73
|
+
props.scriptUrl != null
|
|
74
|
+
? props.scriptUrl
|
|
75
|
+
: scriptUrlTemplate.replace(idToken, props.accountId);
|
|
76
|
+
script.onload = () => {
|
|
77
|
+
window.cookiehub.load(Object.assign(Object.assign({}, opts), { onInitialise(status) {
|
|
78
|
+
const analytics = this.hasConsented('analytics');
|
|
79
|
+
const preferences = this.hasConsented('preferences');
|
|
80
|
+
const marketing = this.hasConsented('marketing');
|
|
81
|
+
const uncategorized = this.hasConsented('uncategorized');
|
|
82
|
+
// only trigger re-render if the consent is different from the default (all false)
|
|
83
|
+
if (analytics || preferences || marketing || uncategorized) {
|
|
84
|
+
setState({
|
|
85
|
+
consent: {
|
|
86
|
+
analytics,
|
|
87
|
+
preferences,
|
|
88
|
+
marketing,
|
|
89
|
+
uncategorized,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
opts.onInitialise && opts.onInitialise.call(this, status);
|
|
94
|
+
},
|
|
95
|
+
onAllow(category) {
|
|
96
|
+
if (category === 'necessary') {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
setState((state) => (Object.assign(Object.assign({}, state), { consent: Object.assign(Object.assign({}, state.consent), { [category]: true }) })));
|
|
100
|
+
opts.onAllow && opts.onAllow.call(this, category);
|
|
101
|
+
},
|
|
102
|
+
onRevoke(category) {
|
|
103
|
+
if (category === 'necessary') {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
setState((state) => (Object.assign(Object.assign({}, state), { consent: Object.assign(Object.assign({}, state.consent), { [category]: false }) })));
|
|
107
|
+
opts.onAllow && opts.onAllow.call(this, category);
|
|
108
|
+
}, cookie: Object.assign({ secure: true }, opts.cookie) }));
|
|
109
|
+
moveCookiehubScriptInDomTree();
|
|
110
|
+
};
|
|
111
|
+
props.onError && (script.onerror = props.onError);
|
|
112
|
+
document.body.append(script);
|
|
113
|
+
},
|
|
114
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
115
|
+
[]);
|
|
116
|
+
return (react_1.default.createElement(CookieHubContext.Provider, { value: state }, props.children));
|
|
117
|
+
};
|
|
118
|
+
exports.CookieHubProvider = CookieHubProvider;
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
/**
|
|
121
|
+
* Returns up-to-date cookie consent flags. For use in React components or hook
|
|
122
|
+
* functions.
|
|
123
|
+
*
|
|
124
|
+
* @see https://github.com/reykjavikcity/webtools/tree/v0.1##usecookiehubconsent
|
|
125
|
+
*/
|
|
126
|
+
const useCookieHubConsent = () => { var _a; return ((_a = (0, react_1.useContext)(CookieHubContext)) === null || _a === void 0 ? void 0 : _a.consent) || {}; };
|
|
127
|
+
exports.useCookieHubConsent = useCookieHubConsent;
|