|
| 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 | +} |
0 commit comments