vewe/classvariance
Composer 安装命令:
composer require vewe/classvariance
包简介
Vewe ClassVariance, an all-PHP combined implementation of Class Variance Authority, twMerge, and Tailwind-Variants
README 文档
README
About
Aiming to be an all-PHP combined implementation of Class Variance Authority, twMerge, and Tailwind-Variants.
Supporting the current version of PHP, less one, as a minimum requirement, to keep the package freshly maintained and utilising the latest features of PHP.
Version Strategy
Based on semantic versioning, with the following constraints
- 1.x.x releases support PHP8.4, PHP8.5
- 2.x.x releases support PHP8.5, PHP8.6
- 3.x.x releases support PHP8.6, PHP-Next (tbc)
Major point releases may introduce breaking changes, which will be in the release notes.
Minor point releases should be non-breaking changes and fixes.
Installation
You can install the package via composer:
composer require vewe/classvariance
Usage
Cv, with slots, akin to Tailwind-Variants
You can declare your classes as a single string, space delimited, or you can provided it as an array of strings, or a mix of the two methods, as suits your needs.
When using slots, always declare a 'base' slot consistently in the Cv::new definition. Unlike Tailwind-Variants, Cv does not assume which classes are meant for which slot, you must be declarative.
This is a conscious design decision, as your definition should have an immutable source of truth, instead of relying on script defaults which could change over time.
use Vewe\ClassVariance\Cv; $button = Cv::new( [ 'base' => ['font-semibold', 'border', 'rounded'], 'label' => [''], ], [ 'variants' => [ 'color' => [ 'primary' => [ 'base' => [ 'bg-blue-500', 'border-transparent', 'hover:bg-blue-600' ], 'label' => ['text-white'], ], 'secondary' => [ 'base' => [ 'bg-white', 'border-gray-400', 'hover:bg-gray-100' ], 'label' => ['text-black'], ], ], 'size' => [ 'small' => [ 'base' => ['py-1', 'px-2'], 'label' => ['text-sm'], ], 'medium' => [ 'base' => ['py-2', 'px-4'], 'label' => ['text-base'], ], ], ], 'compoundVariants' => [ [ 'color' => 'primary', 'size' => 'medium', 'class' => [ 'label' => 'uppercase', ], ], ], 'defaultVariants' => [ 'color' => 'primary', 'size' => 'medium', ], ], );
When you don't specify a slot, it defaults to returning the 'base' slot
<button class="<?= $button(); ?>">Button, assumes default slot of 'base'</button>
You can use named properties or you can pass an empty array for $props.
<button class="<?= $button(slot: 'base'); ?>">Button, specifies slot 'base'</button> <button class="<?= $button([], 'base'); ?>">Button, specifies slot 'base'</button>
You can also take advantage of Tailwind-Variants style declarations, for your component to have multiple sub-components.
<button class="<?= $button(props: ['color' => 'secondary', 'size' => 'small']); ?>"> <span class="<?= $button( props: ['color' => 'secondary', 'size' => 'small'], slot: 'label'); ?>"> Submit </span> </button>
Tip
Looking for a comprehensive set of components using Cv?
Take a look at vewe/ui!
Ready for use with TempestPHP or adapt to anything you like.
Cv, without slots
When not using slots, simply omit them from your definitions entirely.
use Vewe\ClassVariance\Cv; $button = Cv::new( ['font-semibold', 'border', 'rounded'], [ 'variants' => [ 'color' => [ 'primary' => [ 'bg-blue-500', 'text-white', 'border-transparent', 'hover:bg-blue-600' ], 'secondary' => 'bg-white text-gray-800 border-gray-400 hover:bg-gray-100', ], 'size' => [ 'small' => ['text-sm', 'py-1', 'px-2'], 'medium' => 'text-base py-2 px-4', ], ], 'compoundVariants' => [ [ 'color' => 'primary', 'size' => 'medium', 'class' => 'uppercase', ], ], 'defaultVariants' => [ 'color' => 'primary', 'size' => 'medium', ], ], );
Equally, do not pass the slot property at all.
<button class="<?= $button(); ?>">The</button> <button class="<?= $button(['color' => 'primary', 'size' => 'medium']); ?>">Same</button> <button class="<?= $button(props: ['color' => 'primary', 'size' => 'medium']); ?>">Button</button>
Merging additional class data
Whether or not you implement slots configuration, if you wish to pass additional classes to be merged one-off into a specific button, you can pass it in either class or className within $props.
<button class="<?= $button(props: ['color' => 'secondary', 'size' => 'small'], slot: 'base'); ?>"> <span class="<?= $button( props: ['color' => 'secondary', 'size' => 'small'], slot: 'label'); ?>"> Submit </span> </button> <button class="<?= $button(props: ['color' => 'secondary', 'size' => 'small']); ?>">Submit</button> <button class="<?= $button( props: ['class' => 'border-red-600', 'color' => 'secondary', 'size' => 'small'], slot: 'base'); ?>"> <span class="<?= $button( props: ['class' => 'text-italic', 'color' => 'secondary', 'size' => 'small'], slot: 'label'); ?>"> Red-border Italic Submit </span> </button> <button class="<?= $button( props: ['class' => 'border-red-600 text-italic', 'color' => 'secondary', 'size' => 'small']); ?>"> Red-border Italic Submit </button>
Testing
composer phpunit
Credits and Acknowledgements
A special thank you to the following, without whom this package would not exist.
Cvbegan as a heavily-modified fork of featureninja/cva by Roj Vroemen and contributors- In turn this was based on Class Variance Authority by Joe Bell
Also a thank you to the following projects, whose own ideas helped shape Cv.
- Tailwind-Variants by Junior Garcia @jrgarciadev, Tianen Pang @tianenpang and contributors
- Tailwind-merge by Romain Monteil which is based on Tailwind-merge-php by Sandro Gehri and contributors
License
The MIT License (MIT). Please see License File for more information.
vewe/classvariance 适用场景与选型建议
vewe/classvariance 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「css」 「tailwind」 「tailwindcss」 「cva」 「utility-css」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vewe/classvariance 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vewe/classvariance 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vewe/classvariance 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Provides a TailwindUI-based skin for the Winter CMS backend.
Asset for Tailwind for YiiFramework v.3.0.
Tailwind loader for Laravel Nova.
Define your own css rule-sets for the TYPO3 CMS backend.
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-08