Skip to content

Commit d7c5822

Browse files
committed
chore: README wip
1 parent 4f6feae commit d7c5822

File tree

1 file changed

+87
-53
lines changed

1 file changed

+87
-53
lines changed

README.md

Lines changed: 87 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Laravel Inline Scripts
22

3-
A Laravel package that provides a simple way to wrap your JavaScript code stored in a file and inline it as custom Blade directive.
3+
A Laravel package that provides a simple way to wrap your JavaScript code stored in a file and inline it as custom Blade directive.
44

5-
Additionally, allows you to pass variables from PHP to JavaScript easily or process the script in a dedicated PHP class.
5+
Allows:
6+
- passing variables from PHP to JavaScript,
7+
- process / modify the script in a dedicated PHP class.
68

7-
## Requirements
9+
Extra - build in **ready-to-use** scripts:
10+
- theme switching script
11+
- _more coming later_
12+
13+
### Requirements
814

915
- PHP 8.2 or newer
1016
- Laravel 9.x or newer (did not test with older versions)
@@ -20,8 +26,10 @@ composer require zmyslny/laravel-inline-scripts
2026
Register a custom Blade directive for your JavaScript file or files, typically in a service provider `AppServiceProvider`:
2127

2228
```php
23-
class AppServiceProvider extends ServiceProvider {
24-
public function boot(): void {
29+
class AppServiceProvider extends ServiceProvider
30+
{
31+
public function boot(): void
32+
{
2533
BladeInlineScripts::takeFiles(
2634
resource_path('js/your-first-script.js'),
2735
resource_path('js/your-second-script.js'),
@@ -44,95 +52,121 @@ Use the Blade directive in your template to inline the scripts:
4452
...
4553
```
4654

47-
## What are Inline Scripts?
55+
### What are Inline Scripts?
4856

4957
Inline scripts are JavaScript code blocks embedded directly into HTML documents. Traditionally, developers manually write these scripts as strings in the `<head>` section or at the end of the `<body>` section:
5058

5159
```html
60+
...
5261
<script>
5362
// Traditional inline script
5463
document.addEventListener('DOMContentLoaded', function() {
5564
console.log('Page loaded');
5665
});
5766
</script>
67+
</head>
5868
```
5969

6070
This package makes it much more convenient by allowing you to keep inline scripts in separate JavaScript files, which enables:
6171

62-
- **Unit testing** your JavaScript code using tools like Vitest or Jest _(see the example section below)_
72+
- **Complex script processing** using dedicated PHP classes _(see example below)_
73+
- **Variable passing** from PHP to JavaScript _(see example below)_
74+
- **Unit testing** your JavaScript code using tools like Vitest or Jest _(see example below)_
6375
- **Better code organization** and maintainability
64-
- **IDE support** with syntax highlighting and error detection
65-
- **Variable passing** from PHP to JavaScript
76+
- **IDE support** with syntax highlighting and error detection in dedicated JS files
6677

67-
### Example Usage with Blade
78+
# Explanation Through Example: Theme switch script
6879

69-
Instead of writing JavaScript directly in your Blade template:
80+
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.
7081

71-
```blade
72-
<!-- Old way -->
73-
<script>
74-
const userId = {{ auth()->id() }};
75-
const theme = '{{ $theme }}';
76-
// ... more JavaScript code
77-
</script>
82+
Step 1: Publish the built-in theme switch scripts:
83+
84+
```bash
85+
php artisan vendor:publish --tag=theme-switch-2-states-js
7886
```
87+
That will copy the scripts to `resources/js/theme-switch-two-states/[theme-init.js, theme-switch.js]`.
7988

80-
You can now use:
89+
`theme-init.js` - initializes the theme based on the user's previous choice stored in `localStorage`.
90+
`theme-switch.js` - a function to toggle between light and dark themes by hitting a selected KEY and saves the choice in `localStorage`.
8191

82-
```blade
83-
<!-- New way -->
84-
@myInlineScript
92+
Step 2: Register the scripts in your `AppServiceProvider`:
93+
94+
```php
95+
class AppServiceProvider extends ServiceProvider
96+
{
97+
public function boot(): void
98+
{
99+
BladeInlineScripts::takeFiles(
100+
[
101+
resource_path('js/theme-switch-two-states/theme-init.js'),
102+
['__DARK__' => 'dark', '__LIGHT__' => 'light'], // variables to replace in the script
103+
],
104+
[
105+
resource_path('js/theme-switch-two-states/theme-switch.js'),
106+
['__DARK__' => 'dark', '__LIGHT__' => 'light', '__TOGGLE_KEY__' => 'd'], // variables to replace in the script
107+
]
108+
)->registerAs('themeSwitchScripts');
109+
}
110+
}
85111
```
86112

87-
## Usage Example: Theme Switch Scripts
113+
Step 3: Use the Blade directive in your template:
88114

89-
The package includes example scripts for theme switching functionality (dark/light theme) with user preference persistence in localStorage, available under the "theme-switch-2-states-all" tag.
115+
```blade
116+
<html>
117+
<head>
118+
...
119+
@themeSwitchScripts
120+
</head>
121+
<body>
122+
...
123+
```
90124

91-
### Publishing Theme Switch Scripts
125+
Now hit the `d` key to toggle between light and dark themes, and your choice will be remembered on subsequent visits.
92126

93-
To use the included theme switch scripts, publish them:
127+
## Advanced Usage: Custom Script Processing
94128

95-
```bash
96-
php artisan vendor:publish --tag="theme-switch-2-states-all"
97-
```
129+
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.
98130

99-
### Adding as JavaScript Files
131+
Create a custom PHP processor class implementing the `RenderableScript` or `ScriptWithPlaceholders` interface and register it using the `BladeInlineScripts::take()` method.
100132

101-
After publishing, you can include the scripts in your Blade templates:
133+
We have a built-in processor for the theme switch scripts as well. To use it, follow these steps:
102134

103-
```blade
104-
@inlineScript('theme-switch-two-states')
135+
Step 1: Publish the built-in theme switch scripts with the PHP processor:
136+
137+
```bash
138+
php artisan vendor:publish --tag=theme-switch-2-states-php
105139
```
106140

107-
### Adding as PHP Classes
141+
That will copy the scripts to `resources/js/theme-switch-two-states/[theme-init.js, theme-switch.js]` and the processor class to `app/Blade/ThemeSwitchTwoStates/[ThemeInitScript.php, ThemeSwitchScript]`.
108142

109-
You can also use the theme switch functionality through PHP classes:
143+
Step 2: Register the scripts in your `AppServiceProvider`:
110144

111145
```php
112-
use LaravelInlineScripts\ThemeSwitch\ThemeSwitchScript;
146+
class AppServiceProvider extends ServiceProvider
147+
{
148+
public function boot(): void
149+
{
150+
BladeInlineScripts::take(
151+
new ThemeInitScript(),
152+
new ThemeSwitchScript('d')
153+
)->registerAs('themeSwitchScripts');
154+
}
155+
}
156+
```
113157

114-
// In your controller
115-
$themeSwitchScript = new ThemeSwitchScript([
116-
'defaultTheme' => 'light',
117-
'storageKey' => 'user-theme-preference'
118-
]);
158+
Step 3: Use the Blade directive in your template as previously shown.
119159

120-
return view('your-view', ['themeScript' => $themeSwitchScript]);
121-
```
160+
Now hit the `d` key to toggle theme.
122161

123-
Then in your Blade template:
162+
## Extra - get the tests for the built-in scripts and PHP processors
124163

125-
```blade
126-
{!! $themeScript->render() !!}
164+
```bash
165+
php artisan vendor:publish --tag=theme-switch-2-states-js-tests
166+
php artisan vendor:publish --tag=theme-switch-2-states-php-tests
127167
```
128168

129-
The theme switch scripts provide:
130-
- Automatic theme detection based on user's system preference
131-
- Toggle functionality between dark and light themes
132-
- Persistence of user choice in localStorage
133-
- Smooth theme transitions
134-
135-
## 📖 License
169+
### License
136170

137171
This package is licensed under the MIT License.
138172

0 commit comments

Comments
 (0)