Skip to main content

CookieConsent

A component that displays a cookie consent banner at the bottom of the screen. It checks localStorage to see if the user has already accepted or refused cookies.

Component Preview​

Props​

PropTypeDescription
messageReactNodeCustom message content. Defaults to a standard cookie message.
acceptButtonTextstringText for the accept button. Default: Accept.
refuseButtonTextstringText for the refuse button. Default: Refuse.
onAccept() => voidCallback function when the accept button is clicked.
onRefuse() => voidCallback function when the refuse button is clicked.
privacyPolicyLinkstringURL for the privacy policy link.
privacyPolicyTextstringText for the privacy policy link. Default: Privacy Policy.
storageKeystringlocalStorage key to store consent status. Default: sankhya_ui_cookie_consent.
classNamestringOptional CSS class name for the wrapper.

Usage Example​

import { CookieConsent, UserProvider } from '@sankhyatronics/sankhya-ui';

function App() {
return (
<UserProvider>
<CookieConsent
privacyPolicyLink="/privacy"
onAccept={() => console.log('Accepted')}
/>
<MainContent />
</UserProvider>
);
}

JSON Data Example​

{
"type": "CookieConsent",
"data": {
"privacyPolicyLink": "https://example.com/privacy",
"storageKey": "cookie_consent_demo_default"
}
}

Controlling Behavior​

The CookieConsent component integrates with the UserProvider to store the user's choice. You can access this choice using the useUser hook to conditionally render content or initialize scripts.

import { useUser } from '@sankhyatronics/sankhya-ui';
import { useEffect } from 'react';

const AnalyticsComponent = () => {
const { cookieConsent } = useUser();

useEffect(() => {
if (cookieConsent === 'accepted') {
// Initialize analytics
console.log('Initializing Analytics...');
}
}, [cookieConsent]);

if (cookieConsent !== 'accepted') return null;

return <div>Analytics Active</div>;
};