Skip to content

Commit 5442d29

Browse files
committed
chore: README updated
1 parent 225b39a commit 5442d29

File tree

1 file changed

+51
-41
lines changed

1 file changed

+51
-41
lines changed

README.md

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,61 @@ This package makes it much more convenient by allowing you to keep inline script
7373

7474
- **Complex script processing** using dedicated PHP classes _(see example below)_
7575
- **Variable passing** from PHP to JavaScript _(see example below)_
76-
- **Unit testing** your JavaScript code using tools like Vitest or Jest _(see extra section below)_
76+
- **Unit testing** your JavaScript code using tools like Vitest or Jest _(see bonus section below)_
7777
- **Better code organization** and maintainability
7878
- **IDE support** with syntax highlighting and error detection in dedicated JS files
7979

8080
# Explanation Through Example: Theme switch script
8181

8282
Modern websites often provide users with the ability to switch between light and dark themes. In such cases, you might want to remember the user's choice using `localStorage` and apply the selected theme on page load. To avoid **FOUC** (Flash of Unstyled Content), you can use _inline script_ to set the theme before the page fully loads.
8383

84-
**Step 1**: Publish the built-in theme switch scripts:
84+
### Using prepared PHP classes
85+
86+
Add the following to your `AppServiceProvider`:
87+
88+
```php
89+
use Zmyslny\LaravelInlineScripts\Ready\ThemeSwitchTwoStates\InitScript;
90+
use Zmyslny\LaravelInlineScripts\Ready\ThemeSwitchTwoStates\SwitchScript;
91+
92+
class AppServiceProvider extends ServiceProvider
93+
{
94+
public function boot(): void
95+
{
96+
BladeInlineScripts::take(
97+
new InitScript(),
98+
new SwitchScript('d')
99+
)->registerAs('themeSwitchScripts');
100+
}
101+
}
102+
```
103+
104+
and insert a newly created custom Blade directive in your template:
105+
106+
```blade
107+
<html>
108+
<head>
109+
...
110+
@themeSwitchScripts
111+
</head>
112+
<body>
113+
...
114+
```
115+
116+
Now hit the `d` key to toggle between light and dark themes, and your choice will be remembered on subsequent visits.
117+
118+
### Using JS code directly
119+
120+
**Step 1**: Publish the built-in scripts:
85121

86122
```bash
87123
php artisan vendor:publish --tag=theme-switch-2-states-js
88124
```
125+
89126
That will copy the scripts to `resources/js/theme-switch-two-states/[theme-init.js, theme-switch.js]`.
90127

91128
`theme-init.js` - initializes the theme based on the user's previous choice stored in `localStorage`.
92129
`theme-switch.js` - a function to toggle between light and dark themes by hitting a selected KEY and saves the choice in `localStorage`.
93130

94-
They switch theme by adding/removing a CSS class to/from the `<html>` element.
95-
96131
**Step 2**: Register the scripts in your `AppServiceProvider`:
97132

98133
```php
@@ -103,7 +138,7 @@ class AppServiceProvider extends ServiceProvider
103138
BladeInlineScripts::takeFiles(
104139
[
105140
resource_path('js/theme-switch-two-states/theme-init.js'),
106-
['__DARK__' => 'dark', '__LIGHT__' => 'light'], // variables to replace in the script
141+
['__DARK__' => 'dark'], // variables to replace in the script
107142
],
108143
[
109144
resource_path('js/theme-switch-two-states/theme-switch.js'),
@@ -114,7 +149,7 @@ class AppServiceProvider extends ServiceProvider
114149
}
115150
```
116151

117-
**Step 3**: Use the Blade directive in your template:
152+
**Step 3**: Insert a newly created custom Blade directive in your template:
118153

119154
```blade
120155
<html>
@@ -130,54 +165,29 @@ Now hit the `d` key to toggle between light and dark themes, and your choice wil
130165

131166
## Advanced Usage: Custom Script Processing
132167

133-
You can create a custom PHP class to process or modify your JavaScript code before inlining it. For example, you might want to minify the script or add some dynamic content.
168+
Want to do more advanced processing of your JavaScript code before inlining it?
169+
170+
Create a PHP class that implements one of the following interfaces:
171+
- `RenderableScript` - for scripts without placeholders
172+
- `ScriptWithPlaceholders` - for scripts with placeholders to be replaced with variables
134173

135-
Create a custom PHP processor class implementing the `RenderableScript` or `ScriptWithPlaceholders` interface and register it using the `BladeInlineScripts::take()` method.
174+
And use them with `BladeInlineScripts::take(...)` method.
175+
176+
This package provides base abstract implementations (in the context of getting JS code from files) for each of the interfaces:
136177

137-
I have prepared abstract base implementations for each of the interfaces:
138178
```php
139179
abstract class FromFile implements RenderableScript
140180

141181
abstract class FromFileWithPlaceholders implements ScriptWithPlaceholders
142182
```
143183

144-
To show them in action, I have created PHP processors extending the base classes for the theme switch scripts:
145-
146-
**Step 1**: Publish the built-in theme switch scripts with the PHP processor:
147-
148-
```bash
149-
php artisan vendor:publish --tag=theme-switch-2-states-php
150-
```
151-
152-
That will copy the scripts to `resources/js/theme-switch-two-states/[theme-init.js, theme-switch.js]` and the processors classes to `app/Blade/ThemeSwitchTwoStates/[ThemeInitScript.php, ThemeSwitchScript]`.
153-
154-
**Step 2**: Register the scripts in your `AppServiceProvider`:
155-
156-
```php
157-
class AppServiceProvider extends ServiceProvider
158-
{
159-
public function boot(): void
160-
{
161-
BladeInlineScripts::take(
162-
new ThemeInitScript(),
163-
new ThemeSwitchScript('d')
164-
)->registerAs('themeSwitchScripts');
165-
}
166-
}
167-
```
168-
169-
**Step 3**: Use the Blade directive in your template as previously shown.
170-
171-
Now hit the `d` key to toggle theme.
172-
173-
## Extra - get the unit tests for JS scripts and PHP processors
184+
## Bonus - Unit tests for JS scripts
174185

175186
```bash
176187
php artisan vendor:publish --tag=theme-switch-2-states-js-tests
177-
php artisan vendor:publish --tag=theme-switch-2-states-php-tests
178188
```
179189

180-
You can also publish all the assets mentioned at once:
190+
You can also publish JS code with test files at once:
181191

182192
```bash
183193
php artisan vendor:publish --tag=theme-switch-2-states-all

0 commit comments

Comments
 (0)