0.0.6 • Published 1 year ago

twurth.cookiebanner v0.0.6

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Cookie Banner Component

This is a project generated with Stencil in combination with tailwindcss for building a standalone Cookie Banner Web Component. This compontent should be reusable for different projects and easily configurable.

Contents

Used frameworks

Getting Started

To start building this cookie banner web component, clone this repo to a new directory:

git clone https://git.rheinenergie.com/rheinenergie-com-relaunch/baellebad/reusable-cookie-banner.git my-cookie-banner-component
cd my-cookie-banner-component
git remote rm origin

and run:

npm install
npm start

To build this component for production, run:

npm run build

To make tailwindcss listen to changes in the layout, run:

npx tailwindcss -i ./src/directives.css -o ./www/build/cookiebanner.css --watch

Using this component

This component will be uploaded into our CDN soon, then it will be available for individual use. The documentation will then be found here.

CDN

Comming soon...

Configure this component

This component can be configured with its attributes and with slots which will be integrated in the component layout.

Attributes

  • Can be assigned directly within the HTML tag:

    <cookie-banner-component font-family-regular="Arial"></cookie-banner-component>
  • Can be assigned and mutated dynamically with JavaScript:

    document.querySelector('cookie-banner-component').fontFamilyRegular = 'Arial'

    or

    document.querySelector('cookie-banner-component').setAttribute('font-family-regular', 'Arial')

Slots

  • Can be assigned within the HTML component:
    <cookie-banner-component>
      <h1 slot="topic">Cookies</h1>
    </cookie-banner-component>
  • Can be assigned dynamically with JavaScript:
    const slotNode = document.createElement('h1')
    slotNode.slot = 'topic'
    slotNode.textContent = 'Cookies'
    document.querySelector('cookie-banner-component').appendChild(slotNode)
    or mutated:
    document.querySelector('cookie-banner-component [slot="topic"]').textContent = 'Cookies'

Configuration Options

There are different options for attributes and slots which can be used to configure the component. These options are listed below.

Attributes

Attribute nameAttribute typeAttribute default value
img-srcstring'assets/default.svg'
topicstring'Cookies'
descriptionstring'Entscheiden Sie ob die Cookie Einstellungen übernommen oder abgelehnt werden sollen.'
font-family-regularstring'' (Default browser font family)
font-family-boldstring'' (Default browser font family)
color-backdropstring'rgba(0, 0, 0, 0.7)'
color-bgstring'#ffffff'
color-textstring'#000000'
color-primarystring'#000000'
color-secondarystring'#000000'
color-on-primarystring'#ffffff'
color-on-secondarystring'#ffffff'
actionsstring'primary, secondary' (A set of custom actions, can be reassigned for custom use)
show-confirmationbooleanfalse
confirmation-timeoutnumber3000
cookie-storageNamestring'cookiesAccepted'
cookie-domainstring'rheinenergie.com'
ignore-default-actionbooleanfalse
showbooleanDepending on cookie state (When ignore-default-action = true => Default value: false)

Slots

Slot nameSlot default value
topic'Cookies'
description'Entscheiden Sie ob die Cookie Einstellungen übernommen oder abgelehnt werden sollen.'
actionType-action-text''
confirmation-topic'Bestätigung'
confirmation-description'Ihre Cookie-Einstellungen wurden erfolgreich gespeichert.'
actionType-confirmation-topicconfirmation-topic
actionType-confirmation-descriptionconfirmation-description

Listening to action events

The Cookie Banner is emitting an event when a button is clicked (an action is triggered), this event can be accessed with JavaScript and handled individually. The event emitter will return the triggered event with further details to this event, e.g. the button name which is triggered.

document.querySelector('cookie-banner-component').addEventListener('action', (evt) => {
    // The component is accessible with the event target
    const component = evt.currentTarget || evt.target;
    // Details stored in the custom event details
    const eventDetails = evt.detail;
    // The original pointer event is given in the event details
    const originalEvent = eventDetails.event;
    // To check which action is triggered the action type is transferred
    // in the event details as a string, e.g. 'primary' or 'secondary'
    const triggeredAction = eventDetails.type;
});

Example for custom events to listen to

Custom events can be created with the actions attribute of the component which is a type of string. Separated by a "," there could be multiple actions given.

  • Directly assigned with HTML:

    <cookie-banner-component actions="primary, secondary, tertiary"></cookie-banner-component>
  • Dynamically assigned with JavaScript:

    document.querySelector('cookie-banner-component').actions = 'primary, secondary, tertiary'

    or

    document.querySelector('cookie-banner-component').setAttribute('actions', 'primary, secondary, tertiary')

With this assignment it can be listened to this custom assigned action types:

document.querySelector('cookie-banner-component').addEventListener('action', (evt) => {
    switch (evt.detail.type) {
        case 'primary':
            break;
        
        case 'secondary':
            break;

        case 'tertiary':
            break;

        default:
            break;
    }
});

Recommandations

Hide component while undefined

When using slots in a web component the html will usually be rendered before the slots are placed in the web component. So the slot content could appear on the page for a small time frame. To avoid this, there is a CSS property which can be used to hide the element as long as it is not defined.

cookie-banner-component
{
    display: none;
}

cookie-banner-component:defined
{
    display:block;
}

Access component styling from outsite

Add extra styling from without the components shadow dom to customize the component even more. Parts that can be styled externally:

Part to be styledDescription of styled partLocationContaining
backdropBackdrop of the componentTop levelcontent
contentComponent contentbackdrophead, main and footer
headComponent content headcontentlogo
mainComponent content maincontenttopic and description
footerComponent content footercontentaction container
logoLogo imagehead
topicComponent topicmain
descriptionComponent descriptionmain
action-containerContainer containing action buttonsfooteraction(s)
actionEvery actionaction container
actionType-actionSpecific actionaction container
confirmationComponent confirmationbackdropconfirmation-content
confirmation-contentConfirmation contentconfirmationconfirmation-topic, confirmation-description
confirmation-topicConfirmation content topicconfirmation-content
confirmation-descriptionConfirmation content descriptionconfirmation-content

How to access the different parts:

cookie-banner-component::part(backdrop)
{
    ...
}
0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago