CookieConsent
Samtykkedialogen informerer om bruken av informasjonskapsler på nettstedet og lar brukeren velge bort de som ikke er nødvendige for at siden skal fungere.
Kom i gang
Installer
npm i @fremtind/jkl-cookie-consent-react
Bruk
import { CookieConsentProvider, CookieConsent, useCookieConsent } from "@fremtind/jkl-cookie-consent-react";// Importer stilark via JavaScript med CSS-loader.import "@fremtind/jkl-cookie-consent/cookie-consent.min.css";// CookieConsent bruker flere andre komponenter og trenger stilarkene deres for å fungere.import "@fremtind/jkl-button/button.min.css";import "@fremtind/jkl-list/list.min.css";import "@fremtind/jkl-modal/modal.min.css";
React
Live demo
CookieConsent
Egenskaper
Visning
const Example = ({functional = false,statistics = false,marketing = false,}) => {const { openConsentModalWithSettings } = useCookieConsent();return (<><TertiaryButtondata-testid="trigger-cookie-consent"onClick={openConsentModalWithSettings}>Informasjonskapsler</TertiaryButton><CookieConsent blocking onAccept={console.log} /></>);};const CookieConsentModalExample = ({ boolValues }) => {const [hasMounted, setHasMounted] = React.useState(false);React.useEffect(() => {setHasMounted(true);}, []);if (!hasMounted) {return null;}// Start: Kun for demoenclearConsentCookie();// Slutt: Kun for demoenreturn (<CookieConsentProviderfunctional={false}statistics={false}marketing={false}><Examplefunctional={false}statistics={false}marketing={false}/></CookieConsentProvider>);};// Resten av koden her er oppsett for demoen.// Som bruker av pakken skal du bare trenge forholde deg til det over.const COOKIE_NAME = "fremtind-cookie-consent";const getCookie = (name = COOKIE_NAME) => {if (typeof document === "undefined") {return undefined;}const cookie = document.cookie// split a string of cookies into array of cookies.split(";")// split cookies into [name, value].map((s) => s.trim().split("="))// find our golden nugget.find((c) => c[0] === name);if (!cookie) {return undefined;}return cookie;};const getConsentCookie = (adapter) => {const cookie = getCookie();if (cookie) {const consent = JSON.parse(cookie[1]);return consent;}if (adapter) {return adapter();}return undefined;};// 120 daysconst DEFAULT_MAX_AGE = 10368000;const setConsentCookie = (consent,maxAge = DEFAULT_MAX_AGE,name = COOKIE_NAME) => {const cookie = [];cookie.push(`${name}=${JSON.stringify(consent)}`);cookie.push(`max-age=${maxAge}`);cookie.push(`SameSite=Lax`);document.cookie = cookie.join(";");};const shouldShowConsentDialog = (requirement, consent) => {if (!consent) {// check if requirement has truthy values. should show consent if it hasreturn Object.values(requirement).some((requirementValue) => requirementValue);} else {// convert to a map to ease accessing dynamic keysconst consentMap = new Map(Object.entries(consent));// pls spare the CPU of converting to an entries array 3 timesconst requirementEntries = Object.entries(requirement);for (const [name, required] of requirementEntries) {// no need to check more if the value isn't requiredif (!required) {continue;}// cancel and show consent if a value isn't decidedif (consentMap.get(name) === null) {return true;}}return false;}};const convertConsentValueToFormValue = (consent) => {if (!consent) {return undefined;}if (consent === "denied") {return false;}return true;};const convertConsentObjectToBooleans = (consent) => {const defaultConsent = {functional: null,marketing: null,statistics: null,};const consentEntries = Object.entries({ ...defaultConsent, ...consent }).map(([consentName, value]) => [consentName,convertConsentValueToFormValue(value),]);return Object.fromEntries(consentEntries);};const convertBooleanToConsentValue = (formValue) => {if (typeof formValue === "undefined") {return null;}if (formValue === false || String(formValue).toLowerCase() === "false") {return "denied";}return "accepted";};const convertBooleanConsentObjectToConsentObject = (consent) => {const defaultObject = {functional: undefined,marketing: undefined,statistics: undefined,};const consentEntries = Object.entries({ ...defaultObject, ...consent }).map(([consentName, value]) => [consentName, convertBooleanToConsentValue(value)]);return Object.fromEntries(consentEntries);};function clearConsentCookie() {setConsentCookie({functional: null,statistics: null,marketing: null,},-1);}render(<CookieConsentModalExample />);
Bruk av samtykker
Det er opp til hvert enkelt nettsted å sørge for at brukerens valg faktisk respekteres. Under ser du et eksempel på hvordan det kan løses om du bruker Mixpanel.
<CookieConsentblockingonAccept={(v) => {if (v.statistics === "accepted") {mixpanel.opt_in_tracking();saveUserState(user, { statistics: true });} else if (v.statistics === "denied") {mixpanel.opt_out_tracking();saveUserState(user, { statistics: false });}}}/>;
React API
Her finner du en oversikt over props på komponentene i pakken.