-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathContextualNoticeContainer.tsx
More file actions
76 lines (68 loc) · 1.92 KB
/
ContextualNoticeContainer.tsx
File metadata and controls
76 lines (68 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {useState} from 'preact/hooks';
import {purposesOnly} from '../utils/config';
import {useConfig, useManager, useTheme, useTranslations} from '../utils/hooks';
import {template} from '../utils/template';
import {Purpose, TitleLevel} from '../types';
interface ContextualNoticeContainerProps {
purposeId: Purpose['id'];
titleLevel: TitleLevel;
isEnabled: boolean;
}
const ContextualNoticeContainer = ({
purposeId,
titleLevel,
isEnabled
}: ContextualNoticeContainerProps) => {
const config = useConfig();
const manager = useManager();
const t = useTranslations();
const {ContextualNotice} = useTheme();
// There is an intermediate step to notify assistive
// technologies, just after the notice is disabled from
// the inside.
const [isBeingDisabled, setIsBeingDisabled] = useState(false);
if (!isEnabled && !isBeingDisabled) {
return null;
}
const purpose = purposesOnly(config.purposes).find(
({id}) => id === purposeId
);
if (!purpose) {
return null;
}
// When the notice is closed, an invisible placeholder
// is inserted and takes focus to announce the change to
// assistive technologies.
return (
<div className="orejime-Env">
{isEnabled ? (
<ContextualNotice
purpose={purpose}
titleLevel={titleLevel}
privacyPolicyUrl={config.privacyPolicyUrl}
onAccept={() => {
manager.setConsent(purpose.id, true);
setIsBeingDisabled(true);
}}
></ContextualNotice>
) : isBeingDisabled ? (
<div
id={`orejime-ContextualNotice-placeholder--${purpose.id}`}
className="orejime-ContextualNotice-placeholder"
title={template(t.contextual.accepted, {
purpose: purpose.title
}).join('')}
tabIndex={-1}
ref={(self) => {
self?.focus();
}}
onFocusOut={() => {
setIsBeingDisabled(false);
}}
data-testid="orejime-contextual-notice-placeholder"
/>
) : null}
</div>
);
};
export default ContextualNoticeContainer;