Skip to content

Commit 6c19dd2

Browse files
committed
frontend dark mode switcher block
1 parent 086d265 commit 6c19dd2

File tree

17 files changed

+344
-89
lines changed

17 files changed

+344
-89
lines changed

app/base/abstracts/Controllers/FrontendPage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,16 @@ public function getBaseTemplateData(): array
105105

106106
return parent::getBaseTemplateData() + [
107107
'locale' => $this->getCurrentLocale(),
108-
'body_class' => str_replace('.', '-', $this->getRouteName()),
108+
'body_class' => $this->getHtmlBodyClasses($this),
109109
];
110110
}
111111

112+
protected function getHtmlBodyClasses() : string
113+
{
114+
$darkMode = boolval($this->getRequest()->cookies->get('darkmode'));
115+
return str_replace('.', '-', $this->getRouteName()) .' frontend-page' . ($darkMode ? ' dark-mode' : '');
116+
}
117+
112118
/**
113119
* get page region tags html
114120
*
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* SiteBase
5+
* PHP Version 8.3
6+
*
7+
* @category CMS / Framework
8+
* @package Degami\Sitebase
9+
* @author Mirko De Grandis <degami@github.com>
10+
* @license MIT https://opensource.org/licenses/mit-license.php
11+
* @link https://github.com/degami/sitebase
12+
*/
13+
14+
namespace App\Base\Blocks;
15+
16+
use App\Base\Abstracts\Blocks\BaseCodeBlock;
17+
use App\Base\Abstracts\Controllers\BasePage;
18+
use Degami\Basics\Exceptions\BasicException;
19+
use Degami\Basics\Html\TagElement;
20+
use Exception;
21+
use App\App;
22+
23+
/**
24+
* Dark Mode Switcher Block
25+
*/
26+
class DarkModeSwitcher extends BaseCodeBlock
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*
31+
* @param BasePage|null $current_page
32+
* @param array $data
33+
* @return string
34+
* @throws BasicException
35+
*/
36+
public function renderHTML(?BasePage $current_page = null, array $data = []): string
37+
{
38+
try {
39+
$checkbox = $this->getApp()->containerMake(TagElement::class, ['options' => [
40+
'tag' => 'input',
41+
'id' => 'darkmode-selector',
42+
'type' => 'checkbox',
43+
'attributes' => [
44+
'class' => 'paginator-items-choice',
45+
'style' => 'width: 50px',
46+
'checked' => '',
47+
],
48+
]]);
49+
$span = $this->getApp()->containerMake(TagElement::class, ['options' => [
50+
'tag' => 'span',
51+
'attributes' => [
52+
'class' => 'slider',
53+
],
54+
]]);
55+
56+
$label = $this->getApp()->containerMake(TagElement::class, ['options' => [
57+
'tag' => 'label',
58+
'attributes' => [
59+
'class' => 'switch',
60+
],
61+
'children' => [
62+
$checkbox,
63+
$span,
64+
],
65+
]]);
66+
67+
return "<div class=\"darkmode-switch\">" . __('Dark Mode') . ' ' . $label."</div><script type=\"text/javascript\">
68+
(function(\$){
69+
cookieStore.get('darkmode').then(function(data){
70+
\$('#darkmode-selector').prop('checked', data?.value);
71+
});
72+
\$('#darkmode-selector').change(function(evt) {
73+
if (\$(this).prop('checked')) {
74+
\$.cookie('darkmode', \$(this).prop('checked') ? true : null, { expires: 365, path: '/' });
75+
\$('body').addClass('dark-mode');
76+
} else {
77+
\$.removeCookie('darkmode', {path: '/'});
78+
\$('body').removeClass('dark-mode');
79+
}
80+
});
81+
})(jQuery)
82+
</script>";
83+
} catch (Exception $e) {}
84+
return "";
85+
}
86+
}

app/base/tools/Plates/SiteBase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,14 @@ public function canDebug() : bool
494494
{
495495
return $this->getEnvironment()->canDebug();
496496
}
497+
498+
public function getShowLogoOnMenu() : bool
499+
{
500+
return $this->getSiteData()->getShowLogoOnMenu($this->getSiteData()->getCurrentWebsiteId());
501+
}
502+
503+
public function renderSiteLogo() : string
504+
{
505+
return $this->getHtmlRenderer()->renderSiteLogo();
506+
}
497507
}

app/base/tools/Utils/HtmlPartsRenderer.php

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,35 +231,8 @@ public function renderSiteMenu(string $locale): ?string
231231
]]
232232
);
233233

234-
235234
if ($this->getSiteData()->getShowLogoOnMenu($website_id)) {
236-
// add logo
237-
$logo = $this->containerMake(
238-
TagElement::class,
239-
['options' => [
240-
'tag' => 'img',
241-
'attributes' => [
242-
'class' => '',
243-
'title' => $this->getEnvironment()->getVariable('APPNAME'),
244-
'src' => $this->getAssets()->assetUrl('/sitebase_logo.png'),
245-
],
246-
]]
247-
);
248-
249-
$atag = $this->containerMake(
250-
TagElement::class,
251-
['options' => [
252-
'tag' => 'a',
253-
'attributes' => [
254-
'class' => 'navbar-brand',
255-
'href' => $this->getWebRouter()->getUrl('frontend.root'),
256-
'title' => $this->getEnvironment()->getVariable('APPNAME'),
257-
],
258-
]]
259-
);
260-
261-
$atag->addChild($logo);
262-
$menu->addChild($atag);
235+
$menu->addChild($this->renderSiteLogo());
263236
}
264237

265238
// add mobile toggle button
@@ -311,6 +284,37 @@ public function renderSiteMenu(string $locale): ?string
311284
return (string)$menu;
312285
}
313286

287+
public function renderSiteLogo() : TagElement
288+
{
289+
// add logo
290+
$logo = $this->containerMake(
291+
TagElement::class,
292+
['options' => [
293+
'tag' => 'img',
294+
'attributes' => [
295+
'class' => '',
296+
'title' => $this->getEnvironment()->getVariable('APPNAME'),
297+
'src' => $this->getAssets()->assetUrl('/sitebase_logo.png'),
298+
],
299+
]]
300+
);
301+
302+
$atag = $this->containerMake(
303+
TagElement::class,
304+
['options' => [
305+
'tag' => 'a',
306+
'attributes' => [
307+
'class' => 'navbar-brand',
308+
'href' => $this->getWebRouter()->getUrl('frontend.root'),
309+
'title' => $this->getEnvironment()->getVariable('APPNAME'),
310+
],
311+
]]
312+
);
313+
314+
$atag->addChild($logo);
315+
return $atag;
316+
}
317+
314318
/**
315319
* render region blocks
316320
*

app/site/controllers/Frontend/Cms/EventDetail.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ public function process(?RouteInfo $route_info = null, array $route_data = []):
9595
/**
9696
* {@inheritdoc}
9797
*
98-
* @return array
99-
* @throws BasicException
98+
* @return string
10099
*/
101-
public function getBaseTemplateData(): array
100+
protected function getHtmlBodyClasses() : string
102101
{
103-
$out = parent::getBaseTemplateData();
104-
$out ['body_class'] = str_replace('.', '-', $this->getRouteName()) . ' event-' . $this->getObject()->id;
105-
return $out;
102+
return parent::getHtmlBodyClasses() . ' event-' . $this->getObject()->id;
106103
}
107104

108105
/**

app/site/controllers/Frontend/Cms/NewsDetail.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ public function process(?RouteInfo $route_info = null, array $route_data = []):
7979
/**
8080
* {@inheritdoc}
8181
*
82-
* @return array
83-
* @throws BasicException
82+
* @return string
8483
*/
85-
public function getBaseTemplateData(): array
84+
protected function getHtmlBodyClasses() : string
8685
{
87-
$out = parent::getBaseTemplateData();
88-
$out ['body_class'] = str_replace('.', '-', $this->getRouteName()) . ' news-' . $this->getObject()->id;
89-
return $out;
86+
return parent::getHtmlBodyClasses() . ' news-' . $this->getObject()->id;
9087
}
9188

9289
/**

app/site/controllers/Frontend/Cms/Page.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,11 @@ public function getPageId(): int
170170
/**
171171
* {@inheritdoc}
172172
*
173-
* @return array
174-
* @throws BasicException
175-
* @throws DependencyException
176-
* @throws NotFoundException
173+
* @return string
177174
*/
178-
public function getBaseTemplateData(): array
175+
protected function getHtmlBodyClasses() : string
179176
{
180-
$out = parent::getBaseTemplateData();
181-
$out ['body_class'] = str_replace('.', '-', $this->getRouteName()) . ' page-' . $this->getObject()->id;
182-
return $out;
177+
return parent::getHtmlBodyClasses() . ' page-' . $this->getObject()->id;
183178
}
184179

185180
/**

app/site/controllers/Frontend/Cms/Taxonomy.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,11 @@ public function getTemplateName(): string
5757
/**
5858
* {@inheritdoc}
5959
*
60-
* @return array
61-
* @throws BasicException
62-
* @throws DependencyException
63-
* @throws NotFoundException
60+
* @return string
6461
*/
65-
public function getBaseTemplateData(): array
62+
protected function getHtmlBodyClasses() : string
6663
{
67-
$out = parent::getBaseTemplateData();
68-
$out ['body_class'] = str_replace('.', '-', $this->getRouteName()) . ' taxonomy-' . $this->getObject()->id;
69-
return $out;
64+
return parent::getHtmlBodyClasses() . ' taxonomy-' . $this->getObject()->id;
7065
}
7166

7267
/**

app/site/controllers/Frontend/Commerce/DownloadbleDetail.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ public function process(?RouteInfo $route_info = null, array $route_data = []):
9595
/**
9696
* {@inheritdoc}
9797
*
98-
* @return array
99-
* @throws BasicException
98+
* @return string
10099
*/
101-
public function getBaseTemplateData(): array
100+
protected function getHtmlBodyClasses() : string
102101
{
103-
$out = parent::getBaseTemplateData();
104-
$out ['body_class'] = str_replace('.', '-', $this->getRouteName()) . ' downloadable-' . $this->getObject()->id;
105-
return $out;
102+
return parent::getHtmlBodyClasses() . ' downloadable-' . $this->getObject()->id;
106103
}
107104

108105
/**

app/site/controllers/Frontend/ContactForm.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,11 @@ public function process(?RouteInfo $route_info = null, array $route_data = []):
152152
/**
153153
* {@inheritdoc}
154154
*
155-
* @return array
156-
* @throws BasicException
157-
* @throws DependencyException
158-
* @throws \DI\NotFoundException
155+
* @return string
159156
*/
160-
public function getBaseTemplateData(): array
157+
protected function getHtmlBodyClasses() : string
161158
{
162-
$out = parent::getBaseTemplateData();
163-
$out ['body_class'] = str_replace('.', '-', $this->getRouteName()) . ' contact-' . $this->template_data['object']->id;
164-
return $out;
159+
return parent::getHtmlBodyClasses() . ' contact-' . $this->template_data['object']->id;
165160
}
166161

167162
/**

0 commit comments

Comments
 (0)