devanoxltd/tailwind-class-merge-php
Composer 安装命令:
composer require devanoxltd/tailwind-class-merge-php
包简介
TailwindClassMerge for PHP merges multiple Tailwind CSS classes by automatically resolving conflicts between them
关键字:
README 文档
README
TailwindClassMerge for PHP allows you to merge multiple Tailwind CSS classes and automatically resolves conflicts between classes by removing classes conflicting with a class defined later.
A PHP port of tailwind-merge by dcastil. And clone and improve from tailwind-merge-php by gehrisandro.
Supports Tailwind v3.0 up to v3.4.
If you find this package helpful, please consider sponsoring the maintainer:
- Devanox Private Limited: github.com/sponsors/devanoxltd
Attention: This package is still in early development.
If you are using Laravel, you can use the TailwindClassMerge for Laravel
Table of Contents
Get Started
Requires PHP 8.1+
First, install TailwindClassMerge via the Composer package manager:
composer require devanoxltd/tailwind-class-merge-php
Then, use the TailwindClassMerge class to merge your Tailwind CSS classes:
use TailwindClassMerge\TailwindClassMerge; $tw = TailwindClassMerge::instance(); $tw->merge('text-red-500', 'text-blue-500'); // 'text-blue-500'
You can adjust the configuration of TailwindClassMerge by using the factory to create a new instance:
use TailwindClassMerge\TailwindClassMerge; $instance = TailwindClassMerge::factory() ->withConfiguration([ 'prefix' => 'tw-', ])->make(); $instance->merge('tw-text-red-500', 'tw-text-blue-500'); // 'tw-text-blue-500'
For more information on how to configure TailwindClassMerge, see the Configuration section.
Usage
TailwindClassMerge is not only capable of resolving conflicts between basic Tailwind CSS classes, but also handles more complex scenarios:
use TailwindClassMerge\TailwindClassMerge; $tw = TailwindClassMerge::instance(); // conflicting classes $tw->merge('block inline'); // inline $tw->merge('pl-4 px-6'); // px-6 // non-conflicting classes $tw->merge('text-xl text-black'); // text-xl text-black // with breakpoints $tw->merge('h-10 lg:h-12 lg:h-20'); // h-10 lg:h-20 // dark mode $tw->merge('text-black dark:text-white dark:text-gray-700'); // text-black dark:text-gray-700 // with hover, focus and other states $tw->merge('hover:block hover:inline'); // hover:inline // with the important modifier $tw->merge('!font-medium !font-bold'); // !font-bold // arbitrary values $tw->merge('z-10 z-[999]'); // z-[999] // arbitrary variants $tw->merge('[&>*]:underline [&>*]:line-through'); // [&>*]:line-through // non tailwind classes $tw->merge('non-tailwind-class block inline'); // non-tailwind-class inline
It's possible to pass the classes as a string, an array or a combination of both:
$tw->merge('h-10 h-20'); // h-20 $tw->merge(['h-10', 'h-20']); // h-20 $tw->merge(['h-10', 'h-20'], 'h-30'); // h-30 $tw->merge(['h-10', 'h-20'], 'h-30', ['h-40']); // h-40
Cache
For a better performance, TailwindClassMerge can cache the results of the merge operation.
To activate pass your cache instance to the withCache method.
It accepts any PSR-16 compatible cache implementation.
TailwindClassMerge::factory() ->withCache($cache) ->make();
When you are making changes to the configuration make sure to clear the cache.
Configuration
If you are using Tailwind CSS without any extra config, you can use TailwindClassMerge right away. And stop reading here.
If you're using a custom Tailwind config, you may need to configure TailwindClassMerge as well to merge classes properly.
By default TailwindClassMerge is configured in a way that you can still use it if all the following apply to your Tailwind config:
- Only using color names which don't clash with other Tailwind class names
- Only deviating by number values from number-based Tailwind classes
- Only using font-family classes which don't clash with default font-weight classes
- Sticking to default Tailwind config for everything else
If some of these points don't apply to you, you need to customize the configuration.
This is an example to add a custom font size of "very-large":
TailwindClassMerge::factory()->withConfiguration([ 'classGroups' => [ 'font-size' => [ ['text' => ['very-large']] ], ], ])->make();
For a more detailed explanation of the configuration options, visit the original package documentation.
Contributing
Thank you for considering contributing to TailwindClassMerge for PHP! The contribution guide can be found in the CONTRIBUTING.md file.
TailwindClassMerge for PHP is an open-sourced software licensed under the MIT license.
devanoxltd/tailwind-class-merge-php 适用场景与选型建议
devanoxltd/tailwind-class-merge-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 927 次下载、GitHub Stars 达 12, 最近一次更新时间为 2024 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「css」 「utility」 「class」 「classes」 「utilities」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 devanoxltd/tailwind-class-merge-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 devanoxltd/tailwind-class-merge-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 devanoxltd/tailwind-class-merge-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
A collection of enhancements and utilities for the Silverstripe UserForms module
Extends TYPO3 EXT:seo hreflang functionality
PHP7 utility classes
GHT D-Tools Bundle
统计信息
- 总下载量: 927
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-10
