Skip to content

Commit

Permalink
Update DateTimePicker configuration based on theme. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Nov 26, 2023
1 parent 60bcc28 commit 4f44eb6
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 15 deletions.
1 change: 0 additions & 1 deletion .github/workflows/compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
with:
composer-command: |
composer require yiisoft/yii2:^2.0.49 --prefer-dist --no-progress --no-interaction --no-scripts --ansi
extensions: intl
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ echo $form
)
```

### Dynamic Theme Configuration for tempusDominus Widget Library

This library provides functionality to dynamically configure the tempusDominus widget's theme based on user preferences or predefined settings.

#### Functionality Overview

The library introduces a feature to automatically set up the tempusDominus widget with a theme determined by the user's preference or specified configurations.

##### Automatic Theme Configuration

Upon initialization, the library checks for theme configuration settings.
If the user has specified a theme in the library's configuration, it takes precedence.

##### Browser-Based Theme Detection

If no specific theme is set or the attribute data-bs-theme is absent:
The library uses the prefers-color-scheme media query to detect the user's system preference for `light` or `dark` mode.

##### Applying Theme Configuration to tempusDominus Widget

The library sets the theme configuration `(theme: 'dark' or theme: 'light')` based on the detected or specified theme.
This configuration is then applied to the tempusDominus widget using the library's internal functionalities.

### Properties of the widget

| Property | Type | Description | Default |
Expand All @@ -111,7 +134,7 @@ echo $form
### Translation support

The extension supports translation. You can translate the extension into your language,
for default the extension supports the following languages:
for default the extension supports the following languages.

- Chinese
- English
Expand Down
26 changes: 25 additions & 1 deletion src/DateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,31 @@ private function getScript(): string
$config = json_encode($this->config);

return <<<JS
$("#$this->id").tempusDominus({$config});
const htmlElement = document.querySelector('html');
let theme = htmlElement.getAttribute('data-bs-theme');
const config = JSON.parse('$config');
if (config.display && config.display.theme) {
theme = config.display.theme;
} else if (!theme) {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
theme = prefersDark ? 'dark' : 'light';
}
if (theme === 'dark' || theme === 'auto') {
config.display = {
theme: 'dark',
};
}
if (theme === 'light') {
config.display = {
theme: 'light',
};
}
$("#$this->id").tempusDominus(config);
JS;
}

Expand Down
Loading

0 comments on commit 4f44eb6

Please sign in to comment.