Skip to content

Commit f887c5a

Browse files
committed
allow to disable floating toggle
1 parent 6c88b7f commit f887c5a

File tree

8 files changed

+73
-2
lines changed

8 files changed

+73
-2
lines changed

config/cookie-solution.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
*/
2323
'highlight_color' => null,
2424

25+
/**
26+
* Floating banner toggle
27+
* if false, the banner toggle will not be displayed.
28+
* You need to add "data-cookie-solution-toggle" attribute to an element to open the banner.
29+
*/
30+
'toggle_enabled' => true,
31+
2532
/**
2633
* Cookie toggle position (left or right).
2734
*/

resources/js/components/banner.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ function newEmptyStatus(): AcceptStatus {
3636
export class CookieSolutionBanner extends LitElement {
3737
static styles = [styleSheet];
3838

39+
protected disconnectedController?: AbortController;
40+
3941
@property({ type: String, attribute: 'cookie-name' })
4042
cookieName: string = 'laravel_cookie_solution';
4143

@@ -66,6 +68,9 @@ export class CookieSolutionBanner extends LitElement {
6668
async connectedCallback() {
6769
super.connectedCallback();
6870

71+
this.disconnectedController = new AbortController();
72+
73+
this._initListeners();
6974
await this._loadConfig();
7075
this._loadStatus();
7176
this._loadContrastColor();
@@ -80,6 +85,34 @@ export class CookieSolutionBanner extends LitElement {
8085
}
8186
}
8287

88+
disconnectedCallback() {
89+
this.disconnectedController?.abort();
90+
91+
super.disconnectedCallback();
92+
}
93+
94+
private _initListeners(): void {
95+
document.addEventListener(
96+
'click',
97+
(event) => {
98+
let target = event.target;
99+
100+
if (!(target instanceof HTMLElement)) {
101+
return;
102+
}
103+
104+
if (target.getAttribute('data-cookie-solution-toggle') === null) {
105+
return;
106+
}
107+
108+
event.preventDefault();
109+
110+
this.show();
111+
},
112+
{ signal: this.disconnectedController?.signal },
113+
);
114+
}
115+
83116
private async _loadConfig(): Promise<void> {
84117
this._config = window._cookieSolution;
85118
}
@@ -265,7 +298,7 @@ export class CookieSolutionBanner extends LitElement {
265298
}
266299

267300
if (!this._showModal) {
268-
return this.modalToggle();
301+
return !this._config.toggle_enabled ? null : this.modalToggle();
269302
}
270303

271304
return html`

resources/js/laravel-cookie-solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare global {
2727
}
2828
}
2929

30-
(function() {
30+
(function () {
3131
if (!document.querySelector('cookie-solution-banner')) {
3232
const cookieSolutionBanner = document.createElement('cookie-solution-banner');
3333
document.body.append(cookieSolutionBanner);

resources/js/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface CookieSolutionConfig {
1616
digest: string;
1717
cookie_name: string;
1818
cookie_lifetime: number;
19+
toggle_enabled: boolean;
1920
toggle_position: 'left' | 'right';
2021
cookies: Record<CookiePurpose, ServiceConfig[]>;
2122
integrations?: {

src/CookieSolution.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public function getConfig(): array
129129
'digest' => $this->configDigest(),
130130
'cookie_name' => config('cookie-solution.cookie_name'),
131131
'cookie_lifetime' => config('cookie-solution.cookie_lifetime'),
132+
'toggle_enabled' => config('cookie-solution.toggle_enabled', true),
132133
'toggle_position' => config('cookie-solution.toggle_position', 'right'),
133134
'texts' => [
134135
'tab_consent' => __('cookie-solution::texts.banner.tab_consent'),

workbench/app/Providers/WorkbenchServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,10 @@ public function boot(): void
6464
});
6565

6666
Route::view('manual-add', 'manual-add');
67+
Route::get('custom-toggle', function () {
68+
config()->set('cookie-solution.toggle_enabled', false);
69+
70+
return view('custom-toggle');
71+
});
6772
}
6873
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="{{ app()->getLocale() }}">
2+
<head>
3+
<title>
4+
Cookie Solution custom toggle
5+
</title>
6+
@include('cookie-solution::script')
7+
<script src="https://cdn.tailwindcss.com"></script>
8+
</head>
9+
<body class="font-sans antialiased">
10+
<main class="max-w-screen-lg mx-auto px-4 grid gap-16 py-16">
11+
<div>
12+
<button class="text-lg hover:underline" data-cookie-solution-toggle>
13+
cookie preferences
14+
</button>
15+
</div>
16+
</main>
17+
</body>
18+
</html>

workbench/resources/views/index.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
</nav>
1616
</header>
1717
<main class="max-w-screen-lg mx-auto px-4 grid gap-16 py-16">
18+
<div>
19+
<button class="text-lg hover:underline" data-cookie-solution-toggle>
20+
cookie preferences
21+
</button>
22+
</div>
23+
<hr>
1824
<section>
1925
@include('cookie-solution::cookie-policy')
2026
</section>

0 commit comments

Comments
 (0)