-
Notifications
You must be signed in to change notification settings - Fork 82
SvgIcon component added for SVG icon rendering #2302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sazedul-haque
wants to merge
2
commits into
4.0.0-dev
Choose a base branch
from
v4-svg-icon-component
base: 4.0.0-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| <?php | ||
| /** | ||
| * SvgIcon Component Class. | ||
| * | ||
| * Provides a fluent builder for rendering SVG icons. | ||
| * | ||
| * @package Tutor\Components | ||
| * @author Themeum | ||
| * @link https://themeum.com | ||
| * @since 4.0.0 | ||
| */ | ||
|
|
||
| namespace Tutor\Components; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| /** | ||
| * SvgIcon Component Class. | ||
| * | ||
| * Example usage: | ||
| * ``` | ||
| * SvgIcon::make() | ||
| * ->name( Icon::CHECK ) | ||
| * ->size( 24 ) | ||
| * ->render(); | ||
| * | ||
| * SvgIcon::make() | ||
| * ->name( Icon::DELETE ) | ||
| * ->size( 16 ) | ||
| * ->attr( 'class', 'tutor-icon-secondary' ) | ||
| * ->render(); | ||
| * | ||
| * SvgIcon::make() | ||
| * ->name( Icon::DELETE ) | ||
| * ->size( 16 ) | ||
| * ->color( 'secondary' ) | ||
| * ->render(); | ||
| * ``` | ||
| * | ||
| * @since 4.0.0 | ||
| */ | ||
| class SvgIcon extends BaseComponent { | ||
|
|
||
| /** | ||
| * Icon name. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $name = ''; | ||
|
|
||
| /** | ||
| * Icon width. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @var int | ||
| */ | ||
| protected $width = 16; | ||
|
|
||
| /** | ||
| * Icon height. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @var int | ||
| */ | ||
| protected $height = 16; | ||
| /** | ||
| * Icon color. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $color = ''; | ||
|
|
||
| /** | ||
| * Set icon name. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param string $name Icon name. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function name( string $name ): self { | ||
| $this->name = $name; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set icon width. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param int $width Icon width. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function width( int $width ): self { | ||
| $this->width = $width; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set icon height. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param int $height Icon height. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function height( int $height ): self { | ||
| $this->height = $height; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set icon size (both width and height). | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param int $size Icon size. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function size( int $size ): self { | ||
| $this->width = $size; | ||
| $this->height = $size; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set icon color. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param string $color Icon color (idle|idle-inverse|hover|secondary|subdued|disabled|brand|exception1|exception2|success|exception4|exception5|caution|critical|warning). | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function color( string $color ): self { | ||
| $this->color = $color; | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set custom HTML attribute. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @param string $key Attribute name. | ||
| * @param string $value Attribute value. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function attr( $key, $value ) { | ||
| $this->attributes[ $key ] = esc_attr( $value ); | ||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Get the final icon HTML. | ||
| * | ||
| * @since 4.0.0 | ||
| * | ||
| * @return string HTML output. | ||
| */ | ||
| public function get(): string { | ||
| if ( empty( $this->name ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $icon_path = tutor()->path . 'assets/icons/' . $this->name . '.svg'; | ||
| if ( ! file_exists( $icon_path ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $svg = file_get_contents( $icon_path ); | ||
| if ( ! $svg ) { | ||
| return ''; | ||
| } | ||
|
|
||
| preg_match( '/<svg[^>]*viewBox="([^"]+)"[^>]*>(.*?)<\/svg>/is', $svg, $matches ); | ||
| if ( ! $matches ) { | ||
| return ''; | ||
| } | ||
|
|
||
| list( $svg_tag, $view_box, $inner_svg ) = $matches; | ||
|
|
||
| $this->attributes['width'] = $this->width; | ||
| $this->attributes['height'] = $this->height; | ||
| $this->attributes['viewBox'] = $view_box; | ||
| $this->attributes['fill'] = $this->attributes['fill'] ?? 'none'; | ||
| $this->attributes['role'] = $this->attributes['role'] ?? 'presentation'; | ||
| $this->attributes['aria-hidden'] = $this->attributes['aria-hidden'] ?? 'true'; | ||
|
|
||
| if ( ! empty( $this->color ) ) { | ||
| $color_class = "tutor-icon-{$this->color}"; | ||
| $this->attributes['class'] = trim( ( $this->attributes['class'] ?? '' ) . " {$color_class}" ); | ||
| } | ||
|
|
||
| $attributes = $this->render_attributes(); | ||
|
|
||
| $this->component_string = sprintf( | ||
| '<svg %s>%s</svg>', | ||
| $attributes, | ||
| $inner_svg | ||
| ); | ||
|
|
||
| return $this->component_string; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have simple helper, which support all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This additional class will not add value, rather than make increase codebase and inconsistent code use.