-
Notifications
You must be signed in to change notification settings - Fork 29
/
Icon.php
executable file
·120 lines (108 loc) · 3.84 KB
/
Icon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* @link https://github.com/MacGyer/yii2-materializecss
* @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding
* @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE
*/
namespace macgyer\yii2materializecss\widgets;
use macgyer\yii2materializecss\lib\BaseWidget;
use macgyer\yii2materializecss\lib\Html;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Icon can be used to display a Materialize icon.
*
* To be compatible with GDPR (EU) the MaterializeFontAsset is not loaded automatically via the [[\macgyer\yii2materializecss\assets\MaterializeAsset|MaterializeAsset]]. The font asset requests the Material Icon font from Google servers (as stated in the Materialize docs).
* If you are not affected by GDPR, simply load the [[\macgyer\yii2materializecss\assets\MaterializeFontAsset|MaterializeFontAsset]] in your layout or AppAsset.
*
* Otherwise you need to self-host the Material Icon font (i. e. do not request them from Google). You could use `material-icons` (https://www.npmjs.com/package/material-icons) to load the font files, CSS and SCSS from NPM and include them in your build process.
*
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @package widgets
* @see https://materializecss.com/icons.html
*/
class Icon extends BaseWidget
{
/**
* Sets the [[size]] of the icon to "tiny".
*/
public const SIZE_TINY = 'tiny';
/**
* Sets the [[size]] of the icon to "small". This is the default.
*/
public const SIZE_SMALL = 'small';
/**
* Sets the [[size]] of the icon to "medium".
*/
public const SIZE_MEDIUM = 'medium';
/**
* Sets the [[size]] of the icon to "large".
*/
public const SIZE_LARGE = 'large';
/**
* Sets the [[position]] of the icon to "left".
*/
public const POSITION_LEFT = 'left';
/**
* Sets the [[position]] of the icon to "right".
*/
public const POSITION_RIGHT = 'right';
/**
* @var string the name of the icon.
*
* @see https://materializecss.com/icons.html
*/
public $name;
/**
* @var string the position of the icon.
*
* Currently "left" and "right" are natively supported by Materialize, but you can set this property to a custom string
* which will be added to the HTML class attribute and thus can be individually styled.
*/
public $position;
/**
* @var string the size of the icon.
*
* The default icon size is "small".
*/
public $size = self::SIZE_SMALL;
/**
* @var array the HTML options for the icon tag.
*
* The following special options are recognized:
*
* - tag: string, defaults to "i"
*
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail)
* for details on how attributes are being rendered.
*/
public $options = [];
/**
* Initializes the widget.
*
* @throws InvalidConfigException if icon name is not specified
*/
public function init()
{
parent::init();
if (!$this->name) {
throw new InvalidConfigException('The icon name must be specified.');
}
Html::addCssClass($this->options, ['widget' => 'material-icons']);
if ($this->position) {
Html::addCssClass($this->options, ['material-icon-position' => $this->position]);
}
if ($this->size) {
Html::addCssClass($this->options, ['material-icon-size' => $this->size]);
}
}
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
*/
public function run()
{
$tag = ArrayHelper::remove($this->options, 'tag', 'i');
return Html::tag($tag, $this->name, $this->options);
}
}