Skip to content

Commit

Permalink
Merge pull request #353 from dabapps/cookie-banner-max-age
Browse files Browse the repository at this point in the history
Set optional max age for cookie banner
  • Loading branch information
Nikolaev Tomov authored Apr 22, 2021
2 parents 245a011 + b173157 commit 03e2ad7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dabapps/roe",
"version": "0.13.0",
"version": "0.13.1",
"description": "A collection of React components, styles, mixins, and atomic CSS classes to aid with the development of web applications.",
"main": "dist/js/index.js",
"types": "dist/js/index.d.ts",
Expand Down
12 changes: 11 additions & 1 deletion src/ts/components/banners/cookie-banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,30 @@ export type CookieBannerProps = {
* @default 'bottom'
*/
position?: 'top' | 'bottom';
/**
* Sets maximum age for the cookie in seconds
* @default 60 * 60 * 24 * 365
*/
maxAge?: number;
} & OptionalComponentPropAndHTMLAttributes;

/**
* A [Banner](#banner) component that is permanently dismissed after setting a cookie.
* This component takes a render prop, which can be a component or function, that is passed a dismiss prop
* which you can then apply as an onClick prop to an element of your choice.
*/

const A_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;

const CookieBanner = (props: CookieBannerProps) => {
const [dismissed, setDismissed] = React.useState(
Boolean(cookie.parse(document.cookie)['cookies-accepted'])
);

const setCookie = () => {
document.cookie = cookie.serialize('cookies-accepted', 'true');
document.cookie = cookie.serialize('cookies-accepted', 'true', {
maxAge: props.maxAge || A_YEAR_IN_SECONDS,
});
setDismissed(true);
};

Expand Down

0 comments on commit 03e2ad7

Please sign in to comment.