tanthammar/livewire-window-size 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tanthammar/livewire-window-size

Composer 安装命令:

composer require tanthammar/livewire-window-size

包简介

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

README 文档

README

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() || mobileDevice()) {
 //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 to avoid unnecessary server side code execution, duplicated html and rendering 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

//Mobile device detection based on request header.
mobileDevice()

//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.

tanthammar/livewire-window-size 适用场景与选型建议

tanthammar/livewire-window-size 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.47k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2021 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「alpine」 「breakpoints」 「livewire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tanthammar/livewire-window-size 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tanthammar/livewire-window-size 我们能提供哪些服务?
定制开发 / 二次开发

基于 tanthammar/livewire-window-size 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 23.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 1
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-18