承接 micropackage/classnames 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

micropackage/classnames

最新稳定版本:1.0.0

Composer 安装命令:

composer require micropackage/classnames

包简介

Simple PHP utility class for conditionally joining class names together.

README 文档

README

BracketSpace Micropackage Latest Stable Version PHP from Packagist Total Downloads License

Micropackage logo

🧬 About ClassNames

This package contains simple utility class for conditionally joining html classNames. It was inspired by the JavaScript classnames package.

💾 Installation

composer require micropackage/classnames

🕹 Usage

The Micropackage\ClassNames\ClassNames class constructor takes any number of arguments which can be a string or an array. String arguments and values of string arrays will be used unconditionally. If an argument is an array with string keys, keys will be used as classnames if the value associated with a given key is truthy.

use Micropackage\ClassNames\ClassNames;

new ClassNames( 'foo', 'bar' ); // => 'foo bar'
new ClassNames( 'foo', [ 'bar' => true ] ); // => 'foo bar'
new ClassNames( [ 'foo' => true, 'bar' => false ] ); // => 'foo'
new ClassNames( 'foo', [ 'foo' => false ] ); // => ''
new ClassNames( [ 'foo', 'bar' => false, 'baz' ] ); // => 'foo baz'

Full example

<?php
/**
 * Example WordPress template using ACF
 */

use Micropackage\ClassNames\ClassNames;

$text_color = get_filed( 'text-color' );

$classnames = new ClassNames(
	'main-hero',
	[
		'has-background'          => get_filed( 'has-background' ), // Conditionally add background class
		"has-{$text_color}-color" => $text_color, // Only add color class if color is not null
	]
);
?>

<div class="<?php echo $classnames; ?>">
	<!-- (...) -->
</div>

Methods

add

Adds classNames to the current set. Accepts any number of arguments, just like the constructor.

$classnames = new ClassNames( 'foo' );

if ( is_bar() ) {
	$classnames->add( 'bar', [ 'baz' => is_baz() ] );
}
Returns array

All included classnames.

remove

Removes classNames from the current set. Accepts any number of arguments which can be a string or an array of strings.

$classnames = new ClassNames( 'foo', 'bar', 'baz', 'duck' );

if ( ! is_bar() ) {
	$classnames->remove( 'bar', [ 'baz', 'duck' ] );
}
Returns array

All included classnames.

build

Creates string from current classNames set.

$classnames = new ClassNames( 'foo', [ 'bar' => true, 'baz' => false ] );

$result = $classnames->build(); // => 'foo bar'
Returns string

buildAttribute

Creates string with HTML class attribute from current classNames set.

Params

string $before Optional prefix

string $after Optional suffix

$classnames = new ClassNames( 'foo', [ 'bar' => true, 'baz' => false ] );

$result = $classnames->buildAttribute( ' ', ' tabndex="-1"'); // => ' class="foo bar" tabindex="-1"'
Returns string

Static methods

ClassName::get

Accepts arguments like constructor and returns a className string. This is a short equivalent of creating an instance and calling $instance->build().

$result = ClassNames::get( 'foo', [ 'bar' => true, 'baz' => false ] ); // => 'foo bar'
Returns string

ClassName::getAttribute

Accepts arguments like constructor and returns a class attribute string. This is a short equivalent of creating an instance and calling $instance->buildAttribute().

It's possible to pass an array with keys before and/or after as one of arguments.

$result = ClassNames::getAttribute(
	'foo',
	[
		'bar' => true,
		'baz' => false
	],
	[
		'before' => 'prefix ',
		'after'  => ' sufix',
	]
); // => 'prefix class="foo bar" sufix'
Returns string

ClassName::print

Echoes the result of ClassName::get.

ClassNames::print( 'foo', [ 'bar' => true, 'baz' => false ] ); // echoes 'foo bar'
Returns void

ClassName::printAttribute

Echoes the result of ClassName::getAttribute.

ClassNames::getAttribute(
	'foo',
	[
		'bar' => true,
		'baz' => false
	],
	[
		'before' => 'prefix ',
		'after'  => ' sufix',
	]
); // echoes 'prefix class="foo bar" sufix'
Returns void

📦 About the Micropackage project

Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.

The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.

Micropackages are maintained by BracketSpace.

📖 Changelog

See the changelog file.

📃 License

GNU General Public License (GPL) v3.0. See the LICENSE file for more information.

© Credits

This package was inspired by the JavaScript classnames by Jed Watson.

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 1
  • 开发语言: HTML

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2021-04-12