Overview
Features
SimpleGDPR is a lightweight and easy-to-use JS plugin for GDPR consent. It features:
- Simple API.
- Built in themes.
- Customizable via CSS.
- Mobile ready.
- No dependencies.
In addition, the plugin comes with a predefined consent message, which can be easily changed. The built-in themes were created with UX in mind, with the goal of being clear, intuitive, and pretty.
Getting Started
Option 1: CDN
Include both the script and css from the CDN in your HTML document:
<script src="https://unpkg.com/simple-gdpr@1.1.5/dist/simplegdpr.umd.js"></script>
<link href="https://unpkg.com/simple-gdpr@1.1.5/dist/simplegdpr.min.css" rel="stylesheet">
It's recommended to place this at the bottom of your <body> and before your own scripts.
Option 2: Package Manager
npm install simple-gdpr
You can then import into your bundle:
import SimpleGDPR from 'simple-gdpr';
You will also need to import the css file. e.g. with Rollup or Webpack:
import 'simple-gdpr/dist/simplegdpr.min.css';
Basic Usage
Hereβs how easy it is to get started. This is the default SimpleGDPR when given no options.
const notice = new SimpleGDPR();
You can easily customize this behaviour by passing options (see options). In this example, we will close the notice, display an alert, and execute our analytics/cookies logic. we will execute our cookie/analytics logic, display an alert, and finally close the notice when the button is pressed.
const notice = new SimpleGDPR({
callback: () => {
notice.close();
alert('Hi!');
analytics.start();
},
});
In this example, we will change the title text and provide a link (or function) to our privacy policy details.
const notice = new SimpleGDPR({
title: 'This website uses cookies.',
link: './privacypolicy.html',
});
If we want to change up the styles and themes, we can also pass it in the options.
const notice = new SimpleGDPR({
theme: 'modern',
icons: false,
animation: 'slide',
float: 'bottom-left',
});
For more detailed description on all the options, see the options section. Here's an example of all the possible options.
const notice = new SimpleGDPR({
title: 'Cookies and Cream',
message: 'Do you want some?',
link: null,
icons: true,
theme: 'dark',
animation: 'slide',
float: 'bottom-right',
callback: () => {
notice.close();
setTimeout(() => { notice.open }, 1000);
},
openOnInit: true,
id: {
box: 'my-box-id',
button: 'my-button-id',
},
});
Methods
SimpleGDPR has 8 methods available which allow you to control the notice box.
Open and Close
These will trigger animations (if enabled) and show or close appropriately. Close will set the display to be 'none'. isActive() will return true if its currently open, or false if its closed.
notice.open();
notice.close();
notice.isActive();
Set Callback and Content
setCallback provides an interface to change the event listener attached to the button. setContent only takes in HTMLElements and replaces the entire notice box (title, icons, message and button).
notice.setCallback(() => {
notice.close();
Cookies.set('accepted', 'true');
});
const replaced = document.createElement('div');
replaced.innerText = 'Replaced';
notice.setContent(replaced);
Moving the Box
You can use these methods to move the box. relocate() will move the box to one of the float property positions e.g. 'top-right' . appendTo() takes in the id of the parent element, and appends the box to the parent element.
notice.relocate('top-left');
notice.appendTo('parentContainerId');
Destroy
To permanently destroy the SimpleGDPR notice box and remove from the DOM.
notice.destroy();