You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Skrypt zapewnia funkcjonalność przełączania między jasnym i ciemnym motywem na stronie internetowej, z zachowaniem preferencji użytkownika w `localStorage`. Składa się z dwóch części:
9
+
## What does this script do?
8
10
9
-
### 1. InitScript - Inicjalizacja motywu przy ładowaniu strony
11
+
The script provides functionality for switching between light and dark themes on a website, while preserving user preferences in `localStorage`. It consists of three parts:
10
12
11
-
**Plik:**`init-script.js`
13
+
### 1. InitScript - Theme initialization on page load
12
14
13
-
Skrypt uruchamia się **przed wyrenderowaniem strony**(inline w `<head>`), aby uniknąć efektu **FOUC** (Flash of Unstyled Content).
15
+
**File:**`init-script.js`
14
16
15
-
**Działanie:**
16
-
- Sprawdza `localStorage.colorScheme` i jeśli użytkownik wcześniej wybrał motyw, go stosuje
17
-
- Jeśli brak zapisanej preferencji, sprawdza systemowe ustawienia (`prefers-color-scheme`)
18
-
- Dodaje klasę `dark` do `document.documentElement` gdy motyw ciemny jest aktywny
17
+
The script runs **before the page renders** (inline in `<head>`) to avoid **FOUC** (Flash of Unstyled Content).
19
18
20
-
### 2. SwitchScript - Przełączanie motywu
19
+
**Behavior:**
20
+
- Checks `localStorage.colorScheme` and if the user has previously selected a theme, applies it
21
+
- If there's no saved preference, checks system settings (`prefers-color-scheme`)
22
+
- Adds the `dark` class to `document.documentElement` when dark theme is active
21
23
22
-
**Plik:**`switch-script.js`
24
+
### 2. SwitchScript - Theme switching
23
25
24
-
Skrypt umożliwia przełączanie między motywami poprzez skrót klawiszowy (domyślnie klawisz `d`).
26
+
**File:**`switch-script.js`
25
27
26
-
**Działanie:**
27
-
- Udostępnia funkcję `window.inlineScripts.switchColorScheme()` do programowego przełączania motywu
28
-
- Nasłuchuje skrótu klawiszowego (domyślnie `d`) i przełącza motyw
29
-
- Zapisuje wybraną preferencję w `localStorage`
30
-
- Inteligentnie ignoruje naciśnięcia klawisza gdy fokus jest na polach input, textarea, select lub elementach z contentEditable
28
+
The script enables switching between themes via keyboard shortcut (default key `d`).
31
29
32
-
## Użycie
30
+
**Behavior:**
31
+
- Exposes the `window.inlineScripts.switchColorScheme()` function for programmatic theme switching
32
+
- Listens for keyboard shortcut (default `d`) and switches the theme
33
+
- Saves the selected preference in `localStorage`
34
+
- Intelligently ignores key presses when focus is on input, textarea, select fields or elements with contentEditable
33
35
34
-
### Metoda 1: Przy użyciu klas PHP (zalecane)
36
+
### 3. View with color scheme icons
35
37
36
-
**Krok 1:** Dodaj do swojego `AppServiceProvider`:
38
+
A Blade view with light/dark theme icons that call the `window.inlineScripts.switchColorScheme()` function when clicked.
39
+
40
+
## Usage
41
+
42
+
### Method 1: Using PHP classes
43
+
44
+
**Step 1:** Add to your `AppServiceProvider`:
37
45
38
46
```php
39
47
use Zmyslny\LaravelInlineScripts\Ready\ColorSchemeSwitchTwoStates\InitScript;
@@ -44,14 +52,14 @@ class AppServiceProvider extends ServiceProvider
44
52
public function boot(): void
45
53
{
46
54
BladeInlineScripts::take(
47
-
new InitScript(), // inicjalizuje motyw przy ładowaniu strony
48
-
new SwitchScript('d') // przełącza motyw po naciśnięciu klawisza 'd'
55
+
new InitScript(), // initializes the theme on page load
56
+
new SwitchScript('d') // switches the theme when 'd' key is pressed
49
57
)->registerAs('colorSchemeScripts');
50
58
}
51
59
}
52
60
```
53
61
54
-
**Krok 2:**Wstaw dyrektywę Blade w swoim szablonie:
62
+
**Step 2:**Insert the Blade directive in your template:
55
63
56
64
```blade
57
65
<html>
@@ -63,27 +71,27 @@ class AppServiceProvider extends ServiceProvider
63
71
...
64
72
```
65
73
66
-
**Krok 3 (opcjonalnie):**Dodaj widok z ikonami motywów:
3.**Następne wizyty:**Przy kolejnym ładowaniu strony, `init-script.js`odczyta zapisaną preferencję i zastosuje ją przed wyrenderowaniem strony
180
+
1.**On page load:**`init-script.js`checks the saved preference or system preferences and immediately applies the appropriate theme
181
+
2.**Switching:**After pressing the keyboard shortcut or clicking the icons, `switch-script.js`toggles the`dark`class and saves the choice in`localStorage`
182
+
3.**Subsequent visits:**On the next page load, `init-script.js`will read the saved preference and apply it before the page renders
175
183
176
-
## Testowanie
184
+
## Testing
177
185
178
-
Możesz opublikować testy jednostkowe dla skryptów JS:
0 commit comments