Laravel blade directives and php helpers for serverside rendered content, based on browser window size WITHOUT css. Requires Livewire and AlpineJS.

Overview

Laravel Livewire Window Size and Breakpoints

Laravel blade directives and php helpers for server side rendered content, based on browser window size WITHOUT css. Requires Livewire and AlpineJS

An example to show the purpose of this package:

<?php

if(windowXs()) {
 //execute a tiny Eloquent query and return a minimalistic view
}
if(window2xl()) {
 //execute a huge Eloquent query and return a gigantic view
}

Software License Travis Total Downloads

Requirements

  • php 8
  • Laravel 8
  • Livewire
  • AlpineJS

Do you want a pure Javascript version instead?

There is a Laravel/VanillaJS version of this package. Link >>>

Description

The main purpose of this package is not to avoid duplicated html, but to avoid unnecessary server side code execution, just to render content that will never be seen.

  • AlpineJS syncs the browsers inner width and height, in realtime (debounced 750ms), when the browser window is resized.
  • The corresponding Livewire component will store the values to the Laravel Session.
  • The package has a config/breakpoints file where you set your breakpoints
  • The package provides multiple @blade directives and Laravel helpers()
  • You have access to the browser window width/height via session('windowW') and session('windowH')
  • There is also a HasBreakpoints trait to set and get dynamic breakpoints to override config.
  • You can change the config dynamically with Laravel Config::set(...)

Important note

It's important to understand the difference between the server side rendered breakpoints, that this package provides, and css media queries.

When using css, the content of the page will update in realtime as the user resizes the window, whereas this package debounces a network request.

If you are using a Livewire, the component will update on its next request, otherwise the page will update on the next page request.

It's important that you place the <livewire:breakpoints /> at first point of contact with the user, for your application to look its best. I have it in my app.blade.php and on the login/register pages.

Installation

composer require tanthammar/livewire-window-size

Publish config

php artisan vendor:publish --tag=livewire-window-size

The default settings are based on TailwindCSS breakpoints

'window-width' => [
    // 0 = undefined|false
    // @windowXs (>= 1px && < Sm)
    'Sm' => 640, // => @windowSm (>= 640px && < Md)
    'Md' => 768, // => @windowMd (>= 768px && < Lg)
    'Lg' => 1024, // => @windowLg (>= 1024px && < Xl)
    'Xl' => 1280, // => @windowXl (>= 1280px && < 2xl)
    '2xl' => 1536, // => @window2xl (>= 1536px)
],

Add the component to your layout

  • Add this to all layouts where you want to keep track of the browser window size.
  • You will have access to the browser window width/height via session('windowW') and session('windowH')

Example: app.blade.php

<livewire:breakpoints />

Blade directives

@elsif..., @else..., @end..., @unless... and @endif works with all the directives. Explanation in Laravel docs.

//Browser width, with example values
@windowWidthLessThan(400)
@windowWidthGreaterThan(399)
@windowWidthBetween(400, 1500)

//Browser height, with example values
@windowHeightLessThan(500)
@windowHeightGreaterThan(499)
@windowHeightBetween(400, 900)

//Breakpoints based on config values
@windowXs()
@windowSm()
@windowMd()
@windowLg()
@windowXl()
@window2xl()

Example

@windowXs()
<div>This window is extra small</div>
@endif

@window2xl()
<div>This window is very large</div>
@endif

Helpers

Same name as Blade directives

//Browser width, with example values
windowWidthLessThan(400)
windowWidthGreaterThan(399)
windowWidthBetween(400, 1500)

//Browser height, with example values
windowHeightLessThan(500)
windowHeightGreaterThan(499)
windowHeightBetween(400, 900)

//Breakpoints based on config values
windowXs()
windowSm()
windowMd()
windowLg()
windowXl()
window2xl()

Example php

if(windowXs()) {
 //execute a tiny Eloquent query and return a minimalistic view
}
if(window2xl()) {
 //execute a huge Eloquent query and return a gigantic view
}

HasBreakpoints trait

Add the trait to a component to import config values and get/set custom breakpoints dynamically.

use Tanthammar\LivewireWindowSize\HasBreakpoints;

class Foo extends \Livewire\Component
{
    use HasBreakpoints;
    ...
}

Blade directives test component

Add this to any blade view to test the blade directives

<x-breakpoints::test-windowsize />

πŸ’¬ Let's connect

Discuss with other tall-form users on the official Livewire Discord channel. You'll find me in the "partners/tall-forms" channel.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

You might also like...
πŸ”Œ  Convert Bootstrap CSS code to Tailwind CSS code
πŸ”Œ Convert Bootstrap CSS code to Tailwind CSS code

Tailwindo This tool can convert Your CSS framework (currently Bootstrap) classes in HTML/PHP (any of your choice) files to equivalent Tailwind CSS cla

Collection of agnostic PHP Functions and helpers with zero dependencies to use as foundation in packages and other project

Collection of agnostic PHP Functions and helpers This package provides a lot of very usefull agnostic helpers to use as foundation in packages and oth

Automatically load your helpers in your laravel application.

Laravel AutoHelpers Automatically load your helpers in your laravel application. Installation You can install the package via composer: composer requi

Laravel translation helpers
Laravel translation helpers

Laravel Translator Installation and setup Installation You can install the package via composer: composer require isaeken/laravel-translator Setup You

Laravel Helpers Automatic Loading System

About Laravel Helpers Automatic Load Laravel Helpers Automatic Loading System Doc: Copy the Helpers folder and paste it on app folder Then Go To app/P

Laravel magical helpers such as Controllers / Requests / Models

Laravel Magic provides Abstract Controller, Model, generic Request, Traits, Exceptions and various middlewares in order to generate very easily and quickly API resources from scratch.

Because I can never remember exactly how to autoload my helpers.php file.

Laravel Helpers File (helpers.php) I add a app/helpers.php file to every project for any custom helpers I might want to create. Everytime I go to add

Generate and autoload custom Helpers, Builder Scope, Service class, Trait

laravel-make-extender Generate and autoload custom helpers, It can generate multilevel helpers in the context of the directory. Generate Service class

SEO Helper is a package that provides tools and helpers for SEO (Search Engine Optimization).

SEO Helper By ARCANEDEVΒ© SEO Helper is a package that provides tools and helpers for SEO (Search Engine Optimization). Feel free to check out the rele

Releases(1.2.1)
Owner
Tina Hammar
Laravel packages
Tina Hammar
Source code behind the Laracasts Larabit: Creating and Using Custom Blade Directives

This is the source code behind the Laracasts Larabit: Creating and Using Custom Blade Directives, and features all of the files and code available in that video.

Andrew Schmelyun 1 Nov 12, 2021
Blade UI Kit is a set of renderless components to utilise in your Laravel Blade views

Blade UI Kit is a set of renderless components to utilise in your Laravel Blade views. In all essence, it's a collection of useful utilities, connecting the dots between different parts of the TALL stack. It was made for Blade, Laravel's powerful templating engine.

Blade UI Kit 1.2k Jan 5, 2023
Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline.

Blade Snip Blade Snip allows you to use parts of a blade template multiple times. Basically partials, but inline: <div class="products"> @snip('pr

Jack Sleight 18 Dec 4, 2022
Use Blade templates without the full Laravel framework

blade Use Laravel Blade templates as a standalone component without the full Laravel framework Full documentation is available at http://duncan3dc.git

Craig Duncan 138 Dec 7, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
This package allows you to render livewire components like a blade component, giving it attributes, slots etc

X-livewire This package allows you to render livewire components like a blade component, giving it attributes, slots etc. Assuming you wanted to creat

null 7 Nov 15, 2022
Laravel-comments-livewire - Livewire components for the laravel-comments package

Associate comments and reactions with Eloquent models This package contains Livewire components to be used with the spatie/laravel-comments package. S

Spatie 15 Jan 18, 2022
A TALL (Tailwind CSS, Alpine.js, Laravel and Livewire) Preset for Laravel

Laravel TALL Preset A front-end preset for Laravel to scaffold an application using the TALL stack, jumpstarting your application's development. If yo

Laravel Frontend Presets 1.8k Jan 7, 2023
CSS Exfil helper script to generate injected CSS and corresponding HTML (inspired by mike gualtieri)

The PoC-CSS Exfill Basic Keylogger First of all i was developing bot stuff and i seen attribute=value] [target=_blank] in source code of website. This

Ahsen 6 Apr 2, 2022
This package provides a console command to convert dynamic JS/CSS to static JS/CSS assets.

Laravel Nova Search This package provides a console command to convert dynamic JS/CSS to static JS/CSS assets. Requirements laravel-mix v6.0+ php 7.3+

Akki Khare 3 Jul 19, 2022