PHP code example of missionx-co / cva-php
1. Go to this page and download the library: Download missionx-co/cva-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
missionx-co / cva-php example snippets
use MissionX\ClassVariantAuthority\ClassVariantAuthority;
use MissionX\ClassVariantAuthority\Option;
// Create a button component with variants
$button = ClassVariantAuthority::parse([
'base' => 'text-sm px-3 py-2 rounded-md',
'variants' => [
'color' => [
'primary' => 'bg-blue-500 text-white',
'secondary' => 'bg-gray-200 text-gray-800',
'danger' => 'bg-red-500 text-white',
],
'size' => [
'sm' => 'px-2 py-1 text-xs',
'md' => 'px-3 py-2 text-sm',
'lg' => 'px-4 py-3 text-base',
],
],
'compound_variants' => [
[
'color' => 'primary',
'size' => 'lg',
'class' => 'font-bold',
],
],
'default_variants' => [
'size' => 'md',
'color' => 'primary',
],
]);
// Use the component
echo $button(); // "text-sm px-3 py-2 rounded-md bg-blue-500 text-white"
echo $button(['size' => 'lg']); // "text-sm px-4 py-3 rounded-md bg-blue-500 text-white text-base font-bold"
echo $button(['color' => 'secondary']); // "text-sm px-3 py-2 rounded-md bg-gray-200 text-gray-800"
use MissionX\ClassVariantAuthority\ClassVariantAuthority;
use MissionX\ClassVariantAuthority\CompoundVariants;
use MissionX\ClassVariantAuthority\Variant;
use MissionX\ClassVariantAuthority\Option;
$button = ClassVariantAuthority::make()
->setBase('text-sm px-3 py-2 rounded-md')
->addVariant(
Variant::make('color')
->add('primary', 'bg-blue-500 text-white')
->add('secondary', 'bg-gray-200 text-gray-800')
->add('danger', 'bg-red-500 text-white')
)
->addVariant(
Variant::make('size')
->add('sm', 'px-2 py-1 text-xs')
->add('md', 'px-3 py-2 text-sm')
->add('lg', 'px-4 py-3 text-base')
)
->addCompoundVariants(
CompoundVariants::make()
->where('color', 'primary')
->where('size', 'lg')
->setClasses('font-bold')
)
->setDefaultVariants([
'size' => 'md',
'color' => 'primary',
]);
// Use the component
echo $button(['color' => 'danger', 'size' => 'sm']);
use MissionX\ClassVariantAuthority\Config;
$config = new Config();
$config->tailwindMergeConfig = [
// TailwindMerge configuration
];
// Add cache for better performance
$config->cache = new YourCacheImplementation();
// Create instance with config
$button = ClassVariantAuthority::withConfig($config)
->setBase('text-base')
// Add variants...
;
// Or add config to existing instance
$button = ClassVariantAuthority::make()
->setBase('text-base')
->setConfig($config);
bash
composer