M3's optical size axis redraws a symbol so its strokes look equally heavy at every size; drawing the 24 cut at 20px thins them by about a sixth (styles.md § Icons). `optical` picks the cut — 24 by default, 20 for an icon drawn at 20px or smaller — and combines with `filled`; anything else falls back to 24, so no caller can land on a folder that does not exist. SvgFile::symbol() takes the size as a fourth argument and resolves the `-20` folders; the catalogue is the same in both cuts, so symbolNames() and DesignGuard's icon-name check are unchanged. The callers that size their own icons pass it in a later step. Plan step 10; finding C16. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qwx5USif3wFFmxtHg5U1g9
104 lines
3.8 KiB
PHP
104 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace NoNameWeb\LivewireMaterial\Support;
|
|
|
|
use Illuminate\Support\HtmlString;
|
|
use Illuminate\View\ComponentAttributeBag;
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* Inline SVGs from the package's own folders: Material Symbols and M3 Expressive shapes.
|
|
*
|
|
* Not blade-icons: its view-factory hook registers one Blade component per icon on every
|
|
* request, which for the ~16,500 symbol files here would be ~16,500 registrations each time.
|
|
* A file is read the first time it is drawn and kept for the life of the worker; the
|
|
* contents never change while the application runs, so nothing request-specific is held.
|
|
*/
|
|
class SvgFile
|
|
{
|
|
/** @var array<string, string> */
|
|
protected static array $contents = [];
|
|
|
|
/**
|
|
* One symbol, from the cut drawn for the optical size it will be seen at.
|
|
*
|
|
* The package ships Google's 24 and 20 cuts; the 20 one lives in a `-20` folder under the
|
|
* same names, so a symbol that exists exists in both. Any optical size but 20 draws the
|
|
* standard 24 cut, which is also what a caller who says nothing gets.
|
|
*/
|
|
public static function symbol(string $name, bool $filled, ComponentAttributeBag $attributes, int $optical = 24): HtmlString
|
|
{
|
|
if (preg_match('/^[a-z0-9_]+$/', $name) !== 1) {
|
|
throw new InvalidArgumentException("[{$name}] is not a Material Symbol name. Symbols are named in lowercase with underscores, as on fonts.google.com/icons: `calendar_month`, `arrow_back`.");
|
|
}
|
|
|
|
$folder = ($filled ? 'filled' : 'outlined').($optical === 20 ? '-20' : '');
|
|
|
|
$path = static::directory('svg/symbols/'.$folder)."/{$name}.svg";
|
|
|
|
if (! is_file($path)) {
|
|
throw new InvalidArgumentException("There is no Material Symbol named [{$name}].");
|
|
}
|
|
|
|
return static::render($path, $attributes);
|
|
}
|
|
|
|
public static function shape(string $name, ComponentAttributeBag $attributes): HtmlString
|
|
{
|
|
$path = static::directory('svg/shapes')."/{$name}.svg";
|
|
|
|
if (preg_match('/^[a-z0-9-]+$/', $name) !== 1 || ! is_file($path)) {
|
|
throw new InvalidArgumentException("There is no M3 Expressive shape named [{$name}].");
|
|
}
|
|
|
|
return static::render($path, $attributes);
|
|
}
|
|
|
|
/**
|
|
* M3 Expressive's loading indicator: the morphing, rotating shape, or the shape at rest.
|
|
*/
|
|
public static function loadingIndicator(bool $animated, ComponentAttributeBag $attributes): HtmlString
|
|
{
|
|
return static::render(static::directory('svg/loading-indicator/'.($animated ? 'indeterminate' : 'static').'.svg'), $attributes);
|
|
}
|
|
|
|
/**
|
|
* The names of every Material Symbol the package ships, in either cut: Google draws the
|
|
* same catalogue at optical size 24 and at 20, so one folder answers for all four.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
public static function symbolNames(): array
|
|
{
|
|
return array_map(
|
|
fn (string $file): string => basename($file, '.svg'),
|
|
glob(static::directory('svg/symbols/outlined').'/*.svg') ?: [],
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The names of every shape the package ships.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
public static function shapeNames(): array
|
|
{
|
|
return array_map(
|
|
fn (string $file): string => basename($file, '.svg'),
|
|
glob(static::directory('svg/shapes').'/*.svg') ?: [],
|
|
);
|
|
}
|
|
|
|
protected static function render(string $path, ComponentAttributeBag $attributes): HtmlString
|
|
{
|
|
$svg = static::$contents[$path] ??= trim((string) file_get_contents($path));
|
|
|
|
return new HtmlString(preg_replace('/^<svg\b/', '<svg '.$attributes->toHtml(), $svg, 1));
|
|
}
|
|
|
|
protected static function directory(string $path): string
|
|
{
|
|
return dirname(__DIR__, 2).'/resources/'.$path;
|
|
}
|
|
}
|