Skip to content

Commit

Permalink
Add a config setting to disable the focustrap
Browse files Browse the repository at this point in the history
  • Loading branch information
zoglo committed Dec 13, 2024
1 parent f6eefe0 commit 5517aa9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/BASICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contao_cookiebar:
consider_dnt: false
anonymize_ip: false
consent_log: false
disable_focustrap: false
lifetime: 63072000
storage_key: ccb_contao_token
page_templates:
Expand All @@ -40,7 +41,7 @@ Parameter | Description
`consider_dnt` | Consider "Do not Track" browser setting
`consent_log` | Enables/disables consent logging. With each action by the visitor, information about the made choice of cookies is stored.
`anonymize_ip` | Anonymizes the visitor's IP address for each log entry using [Symfony IP Address Anonymizer](https://symfony.com/blog/new-in-symfony-4-4-ip-address-anonymizer).
`consent_log` | Defines whether the Consent Log is enabled or disabled.
`disable_focustrap` | Can be used to disable the focus trap that was introduced in `1.17`.
`lifetime` | Time in seconds that specifies how long the cookie bar settings apply. If the time has expired, the cookie bar is displayed again. If 0 is passed, the cookie bar will never be displayed again automatically and can only be triggered via the version within the cookie bar configuration. (Default: `63072000` = 2 years)
`storage_key` | The key used for localStorage
`page_templates` | An array with page templates which should be considered. Since version `1.8.2` all templates which start with `fe_page_` are considered by default.
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function getConfigTreeBuilder()
->prototype('scalar')->end()
->end()
->end()
->booleanNode('disable_focustrap')
->defaultFalse()
->end()
->end()
;

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/ContaoCookiebarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('contao_cookiebar.storage_key', $config['storage_key']);
$container->setParameter('contao_cookiebar.iframe_types', $config['iframe_types']);
$container->setParameter('contao_cookiebar.page_templates', $config['page_templates']);
$container->setParameter('contao_cookiebar.disable_focustrap', $config['disable_focustrap']);
}
}
3 changes: 2 additions & 1 deletion src/EventListener/FrontendTemplateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public function onOutputFrontendTemplate(string $buffer, string $template): stri
}

// Add cookiebar script initialization
$strHtml .= sprintf("<script>var cookiebar = new ContaoCookiebar({configId:%s,pageId:%s,hideOnInit:%s,blocking:%s,version:%s,lifetime:%s,consentLog:%s,token:'%s',doNotTrack:%s,currentPageId:%s,excludedPageIds:%s,cookies:%s,configs:%s,texts:{acceptAndDisplay:'%s'}});</script>",
$strHtml .= sprintf("<script>var cookiebar = new ContaoCookiebar({configId:%s,pageId:%s,hideOnInit:%s,blocking:%s,focusTrap:%s,version:%s,lifetime:%s,consentLog:%s,token:'%s',doNotTrack:%s,currentPageId:%s,excludedPageIds:%s,cookies:%s,configs:%s,texts:{acceptAndDisplay:'%s'}});</script>",
$objConfig->id,
$objConfig->pageId,
$objConfig->hideOnInit ? 1 : 0,
$objConfig->blocking ? 1 : 0,
System::getContainer()->getParameter('contao_cookiebar.disable_focustrap') ? 0 : 1,
$objConfig->version,
System::getContainer()->getParameter('contao_cookiebar.lifetime'),
System::getContainer()->getParameter('contao_cookiebar.consent_log') ? 1 : 0,
Expand Down
11 changes: 9 additions & 2 deletions src/Resources/public/scripts/cookiebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let ContaoCookiebar = (function () {
pageId: null,
hideOnInit: false,
blocking: false,
focusTrap: true,
version: null,
lifetime: 63072000,
consentLog: false,
Expand Down Expand Up @@ -70,8 +71,11 @@ let ContaoCookiebar = (function () {
// Register trigger events
registerTriggerEvents();

// Initialize focus trap
initFocusTrap();

if (cookiebar.settings.focusTrap) {
// Initialize focus trap
initFocusTrap();
}

if (cookiebar.settings.blocking) {
// Register inert observer
Expand Down Expand Up @@ -665,6 +669,9 @@ let ContaoCookiebar = (function () {
})
}

if (!cookiebar.settings.focusTrap)
return;

if (state) {
document.addEventListener('keydown', focusTrap);
cookiebar.dom.querySelector('.cc-inner').onanimationend = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/scripts/cookiebar.min.js

Large diffs are not rendered by default.

0 comments on commit 5517aa9

Please sign in to comment.