Skip to content

Commit 5d7cf7a

Browse files
authored
Merge pull request #17 from s-group-dev/script-src
feat: add uiVersion?: string prop to UsercentricsScript component
2 parents 3aefec3 + 7521275 commit 5d7cf7a

9 files changed

Lines changed: 705 additions & 337 deletions

File tree

.gitlab-ci.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
## This package is mirrored to an internal NPM registry
33
#
44

5-
image: node:16-alpine
5+
image: node:18-alpine
66

77
stages:
8-
- release
8+
- release
99

1010
workflow:
11-
rules:
12-
- if: '$CI_COMMIT_TAG =~ /^v\d+.\d+.\d+/'
13-
- if: '$CI_MANUAL_TRIGGER'
11+
rules:
12+
- if: '$CI_COMMIT_TAG =~ /^v\d+.\d+.\d+/'
13+
- if: '$CI_MANUAL_TRIGGER'
1414

1515
release:
16-
stage: release
17-
script:
18-
- npm ci
19-
- npm run build
20-
- npm publish
16+
stage: release
17+
script:
18+
- npm ci
19+
- npm run build
20+
- npm publish

README.md

Lines changed: 96 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ React utils for interacting with the [Usercentrics Browser CMP](https://docs.use
88
npm install --save @s-group/react-usercentrics
99
```
1010

11-
This code is used in production:
12-
13-
-https://www.s-kaupat.fi
14-
1511
## Motivation
1612

1713
While the official `@usercentrics/cmp-browser-sdk` npm module is very useful, it is unnecessarily large and would still require some utility functions built around it to be really useful when working in a React application. It has 10 dependencies and an unpacked filesize size of 1.7 MB.
1814

19-
This package aims to be tiny and simple, and has 0 dependencies and an unpacked filesize of 32.7 KB.
15+
This package aims to be tiny and simple, and has 0 dependencies and an unpacked filesize of 47.2 kB KB.
2016

2117
## Setup
2218

@@ -33,13 +29,13 @@ import { USERCENTRICS_SETTINGS_ID, USERCENTRICS_EVENT_NAME } from './config'
3329
import MyApplication from './app'
3430

3531
ReactDOM.render(
36-
<>
37-
<UsercentricsScript settingsId={USERCENTRICS_SETTINGS_ID} />
38-
<UsercentricsProvider windowEventName={USERCENTRICS_EVENT_NAME}>
39-
<MyApplication /** You can interact with Usercentrics inside the provider */ />
40-
</UsercentricsProvider>
41-
</>,
42-
document.body
32+
<>
33+
<UsercentricsScript settingsId={USERCENTRICS_SETTINGS_ID} />
34+
<UsercentricsProvider windowEventName={USERCENTRICS_EVENT_NAME}>
35+
<MyApplication /** You can interact with Usercentrics inside the provider */ />
36+
</UsercentricsProvider>
37+
</>,
38+
document.body
4339
)
4440
```
4541

@@ -65,7 +61,7 @@ or even import the type from another file:
6561

6662
```ts
6763
declare module '@s-group/react-usercentrics/augmented' {
68-
export type ServiceId = import('../config/usercentrics').ServiceId
64+
export type ServiceId = import('../config/usercentrics').ServiceId
6965
}
7066
```
7167

@@ -87,19 +83,24 @@ Do not declare this globally, but prefer to use the included utility functions i
8783

8884
#### `UsercentricsScript`
8985

90-
The script tag that loads the Usercentrics Browser API.
86+
The script tag that loads the Usercentrics Browser UI and API.
9187

9288
```tsx
93-
interface UsercentricsScriptProps {
89+
interface UsercentricsScriptProps
90+
extends React.DetailedHTMLProps<React.ScriptHTMLAttributes<HTMLScriptElement>, HTMLScriptElement> {
9491
settingsId: string
92+
uiVersion?: string
9593
version?: 'production' | 'preview'
9694
}
9795

9896
/** Default production mode */
99-
() => <UsercentricsScript settingsId="1234" />
97+
;() => <UsercentricsScript settingsId="1234" />
10098

10199
/** Preview mode for development */
102-
() => <UsercentricsScript settingsId="1234" version="preview" />
100+
;() => <UsercentricsScript settingsId="1234" version="preview" />
101+
102+
/* Fixed UI version instead of latest */
103+
;() => <UsercentricsScript settingsId="1234" uiVersion="3.21.1" />
103104
```
104105

105106
#### `UsercentricsProvider`
@@ -157,7 +158,7 @@ interface UsercentricsDialogToggleProps extends ButtonHTMLAttributes<HTMLButtonE
157158
}
158159

159160
/** Basic usage */
160-
() => <UsercentricsDialogToggle>Manage my consents</UsercentricsDialogToggle>
161+
;() => <UsercentricsDialogToggle>Manage my consents</UsercentricsDialogToggle>
161162
```
162163

163164
### Hooks
@@ -167,17 +168,17 @@ interface UsercentricsDialogToggleProps extends ButtonHTMLAttributes<HTMLButtonE
167168
Whether the specific Usercentrics service has been given consent.
168169
Returns `true` or `false` based on consent status, or `null` when unknown (not yet loaded).
169170

170-
**Warning:** it's best to assume no consent until this hook returns `true`
171+
**Warning:** it's best to assume no consent until this hook returns `true`
171172

172173
```tsx
173-
() => {
174-
const hasConsent = useHasServiceConsent('my-service-id')
175-
176-
useEffect(() => {
177-
if (hasConsent) {
178-
loadMyService()
179-
}
180-
}, [hasConsent])
174+
;() => {
175+
const hasConsent = useHasServiceConsent('my-service-id')
176+
177+
useEffect(() => {
178+
if (hasConsent) {
179+
loadMyService()
180+
}
181+
}, [hasConsent])
181182
}
182183
```
183184

@@ -189,14 +190,14 @@ consent status is unknown and will default to `false`,
189190
so no services can be used.
190191

191192
```tsx
192-
() => {
193-
const isFailed = useIsFailed()
194-
195-
useEffect(() => {
196-
if (isFailed) {
197-
console.error('Failed to load consents! This site might not work properly.')
198-
}
199-
}, [isFailed])
193+
;() => {
194+
const isFailed = useIsFailed()
195+
196+
useEffect(() => {
197+
if (isFailed) {
198+
console.error('Failed to load consents! This site might not work properly.')
199+
}
200+
}, [isFailed])
200201
}
201202
```
202203

@@ -205,14 +206,14 @@ so no services can be used.
205206
Returns `true` if Usercentrics has been initialized and consents can be given.
206207

207208
```tsx
208-
() => {
209-
const isInitialized = useIsInitialized()
210-
211-
useEffect(() => {
212-
if (isInitialized) {
213-
console.info('Usercentrics has initialized!')
214-
}
215-
}, [isInitialized])
209+
;() => {
210+
const isInitialized = useIsInitialized()
211+
212+
useEffect(() => {
213+
if (isInitialized) {
214+
console.info('Usercentrics has initialized!')
215+
}
216+
}, [isInitialized])
216217
}
217218
```
218219

@@ -221,16 +222,16 @@ Returns `true` if Usercentrics has been initialized and consents can be given.
221222
Returns `true` if the Usercentrics dialog is currently open on the page.
222223

223224
```tsx
224-
() => {
225-
const isOpen = useIsOpen()
226-
227-
useEffect(() => {
228-
if (isOpen) {
229-
console.debug('Usercentrics dialog is open and probably covers the whole screen')
230-
} else {
231-
console.debug('Usercentrics dialog is now closed')
232-
}
233-
}, [isOpen])
225+
;() => {
226+
const isOpen = useIsOpen()
227+
228+
useEffect(() => {
229+
if (isOpen) {
230+
console.debug('Usercentrics dialog is open and probably covers the whole screen')
231+
} else {
232+
console.debug('Usercentrics dialog is now closed')
233+
}
234+
}, [isOpen])
234235
}
235236
```
236237

@@ -239,42 +240,42 @@ Returns `true` if the Usercentrics dialog is currently open on the page.
239240
Returns `true` if the user has interacted with the Usercentrics dialog and given consent information.
240241

241242
```tsx
242-
() => {
243-
const hasUserInteracted = useHasUserInteracted()
244-
245-
useEffect(() => {
246-
if (hasUserInteracted) {
247-
console.debug('User has interacted with the Usercentrics dialog and given consent information')
248-
} else {
249-
console.debug('User has not interacted with the Usercentrics dialog and not given consent information')
250-
}
251-
}, [hasUserInteracted])
243+
;() => {
244+
const hasUserInteracted = useHasUserInteracted()
245+
246+
useEffect(() => {
247+
if (hasUserInteracted) {
248+
console.debug('User has interacted with the Usercentrics dialog and given consent information')
249+
} else {
250+
console.debug('User has not interacted with the Usercentrics dialog and not given consent information')
251+
}
252+
}, [hasUserInteracted])
252253
}
253254
```
254255

255256
#### `useServiceInfo`
256257

257258
Returns basic info for specific Usercentrics service, or null if not found.
258259

259-
The typing is *not complete* and contains only the info used internally:
260+
The typing is _not complete_ and contains only the info used internally:
260261

261-
- `id` of the service, autogenerated by Usercentrics
262-
- `name` of the service, configured in the admin interface
263-
- `consent.status` of the service
262+
- `id` of the service, autogenerated by Usercentrics
263+
- `name` of the service, configured in the admin interface
264+
- `consent.status` of the service
264265

265266
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesbaseinfo
266267

267268
```tsx
268-
() => {
269-
const serviceInfo = useServiceInfo('my-service-id')
270-
271-
useEffect(() => {
272-
if (!serviceInfo) {
273-
console.error('Service not found for "my-service-id"')
274-
} else {
275-
console.info(`Consent for ${serviceInfo.name}: ${serviceInfo.consent.status}`)
276-
}
277-
}, [serviceInfo])
269+
;() => {
270+
const serviceInfo = useServiceInfo('my-service-id')
271+
272+
useEffect(() => {
273+
if (!serviceInfo) {
274+
console.error('Service not found for "my-service-id"')
275+
} else {
276+
console.info(`Consent for ${serviceInfo.name}: ${serviceInfo.consent.status}`)
277+
}
278+
}, [serviceInfo])
278279
}
279280
```
280281

@@ -283,24 +284,24 @@ See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesbaseinfo
283284
Returns full info for specific Usercentrics service, or null if not found.
284285
This triggers an extra API call and also returns `null` while loading.
285286

286-
The typing is *not complete* and contains only the info used internally:
287+
The typing is _not complete_ and contains only the info used internally:
287288

288-
- `id` of the service, autogenerated by Usercentrics
289-
- `name` of the service, configured in the admin interface
290-
- `consent.status` of the service
291-
- `description` text of the service
289+
- `id` of the service, autogenerated by Usercentrics
290+
- `name` of the service, configured in the admin interface
291+
- `consent.status` of the service
292+
- `description` text of the service
292293

293294
See also https://docs.usercentrics.com/#/cmp-v2-ui-api?id=getservicesfullinfo
294295

295296
```tsx
296-
() => {
297-
const serviceInfo = useServiceFullInfo('my-service-id')
298-
299-
useEffect(() => {
300-
if (serviceInfo) {
301-
console.info(`The ${serviceInfo.name} service is used to ${serviceInfo.description}`)
302-
}
303-
}, [serviceInfo])
297+
;() => {
298+
const serviceInfo = useServiceFullInfo('my-service-id')
299+
300+
useEffect(() => {
301+
if (serviceInfo) {
302+
console.info(`The ${serviceInfo.name} service is used to ${serviceInfo.description}`)
303+
}
304+
}, [serviceInfo])
304305
}
305306
```
306307

@@ -369,7 +370,7 @@ const myService = services.find((service) => service.id === 'my-service-id')
369370
const hasConsent = hasServiceConsent(myService)
370371

371372
if (hasConsent) {
372-
loadMyService()
373+
loadMyService()
373374
}
374375
```
375376

@@ -398,13 +399,15 @@ A method to check if user has interacted with the consent prompt and given conse
398399
```tsx
399400
const userInteracted = hasUserInteracted()
400401
if (userInteracted) {
401-
actionRequiredConsentInfoGiven()
402+
actionRequiredConsentInfoGiven()
402403
}
403404
```
404405

405406
#### `getServicesFromLocalStorage`
407+
406408
A method to get array of all services from local storage
409+
407410
```tsx
408411
const services = getServicesFromLocalStorage()
409412
const myService = services.find((service) => service.id === 'my-service-id')
410-
```
413+
```

jest-setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line import/extensions
2+
import '@testing-library/jest-dom/extend-expect'

jest.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
moduleNameMapper: {
44
'^(\\.{1,2}/.*)\\.js$': '$1',
55
},
6+
setupFilesAfterEnv: ['./jest-setup.ts'],
67
testEnvironment: 'jsdom',
78
testMatch: ['**/*.(spec|test).(ts|tsx|?js)'],
89
transform: {

0 commit comments

Comments
 (0)