Skip to content

Commit 4f6feae

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

File tree

1 file changed

+139
-2
lines changed

1 file changed

+139
-2
lines changed

README.md

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,139 @@
1-
# laravel-inline-scripts
2-
Provides a simple way to wrap you JS code, stored in file or files, and inline it as a custom blade directive.
1+
# Laravel Inline Scripts
2+
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.
4+
5+
Additionally, allows you to pass variables from PHP to JavaScript easily or process the script in a dedicated PHP class.
6+
7+
## Requirements
8+
9+
- PHP 8.2 or newer
10+
- Laravel 9.x or newer (did not test with older versions)
11+
12+
## 🚀 Quick Start
13+
14+
Install the package via Composer:
15+
16+
```bash
17+
composer require zmyslny/laravel-inline-scripts
18+
```
19+
20+
Register a custom Blade directive for your JavaScript file or files, typically in a service provider `AppServiceProvider`:
21+
22+
```php
23+
class AppServiceProvider extends ServiceProvider {
24+
public function boot(): void {
25+
BladeInlineScripts::takeFiles(
26+
resource_path('js/your-first-script.js'),
27+
resource_path('js/your-second-script.js'),
28+
...
29+
)->registerAs('myInlineScripts');
30+
}
31+
}
32+
```
33+
34+
Use the Blade directive in your template to inline the scripts:
35+
36+
```blade
37+
<html>
38+
<head>
39+
...
40+
41+
@myInlineScripts
42+
</head>
43+
<body>
44+
...
45+
```
46+
47+
## What are Inline Scripts?
48+
49+
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:
50+
51+
```html
52+
<script>
53+
// Traditional inline script
54+
document.addEventListener('DOMContentLoaded', function() {
55+
console.log('Page loaded');
56+
});
57+
</script>
58+
```
59+
60+
This package makes it much more convenient by allowing you to keep inline scripts in separate JavaScript files, which enables:
61+
62+
- **Unit testing** your JavaScript code using tools like Vitest or Jest _(see the example section below)_
63+
- **Better code organization** and maintainability
64+
- **IDE support** with syntax highlighting and error detection
65+
- **Variable passing** from PHP to JavaScript
66+
67+
### Example Usage with Blade
68+
69+
Instead of writing JavaScript directly in your Blade template:
70+
71+
```blade
72+
<!-- Old way -->
73+
<script>
74+
const userId = {{ auth()->id() }};
75+
const theme = '{{ $theme }}';
76+
// ... more JavaScript code
77+
</script>
78+
```
79+
80+
You can now use:
81+
82+
```blade
83+
<!-- New way -->
84+
@myInlineScript
85+
```
86+
87+
## Usage Example: Theme Switch Scripts
88+
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.
90+
91+
### Publishing Theme Switch Scripts
92+
93+
To use the included theme switch scripts, publish them:
94+
95+
```bash
96+
php artisan vendor:publish --tag="theme-switch-2-states-all"
97+
```
98+
99+
### Adding as JavaScript Files
100+
101+
After publishing, you can include the scripts in your Blade templates:
102+
103+
```blade
104+
@inlineScript('theme-switch-two-states')
105+
```
106+
107+
### Adding as PHP Classes
108+
109+
You can also use the theme switch functionality through PHP classes:
110+
111+
```php
112+
use LaravelInlineScripts\ThemeSwitch\ThemeSwitchScript;
113+
114+
// In your controller
115+
$themeSwitchScript = new ThemeSwitchScript([
116+
'defaultTheme' => 'light',
117+
'storageKey' => 'user-theme-preference'
118+
]);
119+
120+
return view('your-view', ['themeScript' => $themeSwitchScript]);
121+
```
122+
123+
Then in your Blade template:
124+
125+
```blade
126+
{!! $themeScript->render() !!}
127+
```
128+
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
136+
137+
This package is licensed under the MIT License.
138+
139+

0 commit comments

Comments
 (0)