Skip to content

Commit d74d9a9

Browse files
committed
Add @ICON Blade helper
Also fix heading hierarchy in README.
1 parent 59cbda9 commit d74d9a9

File tree

8 files changed

+173
-7
lines changed

8 files changed

+173
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1515

1616
- Syntax highlighting in README
1717
- Add example markup to starter kit (replaces default Laravel welcome view)
18+
- `@icon` Blade helper (see install notes in `resources/views/elements/icon.blade.php`)
1819

1920
### Changed
2021

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ var dom = require('mode-front-end/resources/assets/js/dom');
244244
- `Visibility`
245245
- `window`
246246

247-
## Event
247+
### Event
248248

249249
- `animationEvents`
250250
- `clearStack`
@@ -253,31 +253,37 @@ var dom = require('mode-front-end/resources/assets/js/dom');
253253
- `throttle`
254254
- `transitionEvents`
255255

256-
## General
256+
### General
257257

258258
- `menuAim`
259259

260-
## HTTP
260+
### HTTP
261261

262262
- `ajax`
263263

264-
## Object
264+
### Object
265265

266266
- `extend`
267267

268-
## Shim
268+
### Shim
269269

270270
- `requestAnimationFrame`
271271

272-
## Typography
272+
### Typography
273273

274274
- `balanceText`
275275
- `shorten`
276276

277-
## Video
277+
### Video
278278

279279
- `youTubeReady`
280280

281+
## Laravel
282+
283+
### Blade Helpers
284+
285+
- `@icon` (see install notes in `resources/views/elements/icon.blade.php`)
286+
281287
---
282288

283289
## TODO
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class IconServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Perform post-registration booting of services.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
Blade::directive('icon', function ($expression) {
18+
return "<?php echo icon({$expression}); ?>";
19+
});
20+
}
21+
22+
/**
23+
* Register bindings in the container.
24+
*
25+
* @return void
26+
*/
27+
public function register()
28+
{
29+
//
30+
}
31+
}

laravel/Support/helpers.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once('helpers/icon.php');

laravel/Support/helpers/icon.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
if (!function_exists('icon')) {
4+
5+
/**
6+
* Render icons.
7+
* @param string $name
8+
* @param array $data
9+
* @return string
10+
*/
11+
function icon($name = '', array $data = [])
12+
{
13+
$data['sprite'] = empty($data['sprite']) ? 'global' : $data['sprite'];
14+
$data['icon'] = $name;
15+
16+
if (!empty($data['attributes'])) {
17+
$attributes = [];
18+
19+
foreach ($data['attributes'] as $attributeName => $attributeValue) {
20+
if (empty($attributeValue)) {
21+
$attributes[] = $attributeName;
22+
continue;
23+
}
24+
$attributes[] = sprintf("%s=\"%s\"", $attributeName, addcslashes($attributeValue, '"'));
25+
}
26+
27+
$data['attributes'] = $attributes;
28+
}
29+
30+
return view('elements.icon', $data)->render();
31+
}
32+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* ## App Config
4+
*
5+
* In `config/app.php`:
6+
*
7+
* ```php
8+
* // Application Service Providers...
9+
* App\Providers\IconServiceProvider::class,
10+
* ```
11+
*
12+
* In `composer.json`:
13+
*
14+
* ```php
15+
* "autoload": {
16+
* "files": [
17+
* "app/Support/helpers.php"
18+
* ]
19+
* }
20+
* ```
21+
*
22+
* Then copy the helper functions, the service provider, and update the autoloader:
23+
*
24+
* ```bash
25+
* # Provider
26+
* cp node_modules/mode-front-end/laravel/Providers/IconServiceProvider.php app/Providers/IconServiceProvider.php
27+
* # Helper
28+
* mkdir -p app/Support/helpers
29+
* cp node_modules/mode-front-end/laravel/Support/helpers.php app/Support/helpers.php
30+
* cp node_modules/mode-front-end/laravel/Support/helpers/icon.php app/Support/helpers/icon.php
31+
* # View
32+
* mkdir -p resources/views/elements
33+
* cp node_modules/mode-front-end/resources/views/elements/icon.blade.php resources/views/elements/icon.blade.php
34+
* # Autoloader
35+
* php artisan optimize
36+
* ```
37+
*
38+
* ## Markup
39+
*
40+
* ```php
41+
* Example:
42+
* @icon('mode-logo')
43+
*
44+
* Example with verbose options:
45+
* @icon('mode-logo', [
46+
* 'sprite' => 'global',
47+
* 'class' => 'u-display-block / u-text-align-center',
48+
* 'attributes' => ['data-name' => 'Example Icon']
49+
* ])
50+
* ```
51+
*
52+
* ## Variables
53+
*
54+
* Required:
55+
*
56+
* - $icon
57+
* - $sprite
58+
*
59+
* Optional:
60+
*
61+
* - $class
62+
* - $attributes
63+
*/
64+
65+
$class = empty($class) ? [] : [$class];
66+
array_unshift($class, 'o-icon o-icon--' . $icon);
67+
$class = implode(' / ', $class);
68+
69+
$attributes = empty($attributes) ? [] : $attributes;
70+
$attributes = implode(' ', $attributes);
71+
72+
?><i class="{{ $class }}" {!! $attributes !!}>
73+
<svg>
74+
<use xlink:href="{{ asset('img/svg/sprites/' . $sprite . '.svg') . '#' . $icon }}"></use>
75+
</svg>
76+
</i>

starter-kit/assets/sass/app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// Objects
1616
@import 'objects/grid';
17+
@import 'objects/icon';
1718

1819
// Components
1920
@import 'components/browser-upgrade';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@include o-icon();
2+
3+
// Sizes
4+
@include o-icon--size(70, 70);
5+
6+
7+
8+
// ------------------------------
9+
// Responsive
10+
// ------------------------------
11+
12+
// Max-width
13+
// @include o-icon--size(70, 70, 'lt-sm');
14+
15+
// Min-width
16+
// @include o-icon--size(70, 70, 'sm');

0 commit comments

Comments
 (0)