Skip to content

Commit

Permalink
refactor: introduce sky-link component
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed Mar 18, 2024
1 parent 42c3cb3 commit 6b514b4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
3 changes: 3 additions & 0 deletions resources/views/components/sky-link.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a {{ $attributes->except('label')->except('hasLabelWrap') }}>
{{ $label }}
</a>
4 changes: 2 additions & 2 deletions src/Classes/LinkRenderers/NavLinkRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function getActiveClass(): string
public function getPreparedLink(string $classes = ''): array
{
return [
'classes' => $classes . ' ' . $this->getActiveClass(),
'class' => $classes . ' ' . $this->getActiveClass(),
'target' => $this->item['data']['target'] ?? '_self',
'link' => $this->getLink(),
'href' => $this->getLink(),
'label' => $this->item['label'],
];
}
Expand Down
23 changes: 5 additions & 18 deletions src/Classes/RenderNavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LaraZeus\Sky\Classes;

use Illuminate\Support\Facades\Blade;
use LaraZeus\Sky\Classes\LinkRenderers\GenericLinkRenderer;
use LaraZeus\Sky\Classes\LinkRenderers\NavLinkRenderer;
use LaraZeus\Sky\SkyPlugin;
Expand All @@ -13,23 +14,6 @@ class RenderNavItem
*/
public static string $defaultRendererClass = GenericLinkRenderer::class;

private static function anchorLink(
string $classes,
string $target,
string $link,
string $label,
): string {
// TODO: make this component based?
// Then it's probably easier for users to further customize this?
return '<a class="' . $classes . '"
target="' . $target . '"
href="' . $link . '"
>' .
$label . // TODO: allow optional wrapping link text in span?
// Or maybe support this customization via components?
'</a>';
}

public static function render(array $item, string $class = ''): string
{
$itemType = $item['type'];
Expand All @@ -47,6 +31,9 @@ public static function render(array $item, string $class = ''): string
/**
* @var NavLinkRenderer $renderer
*/
return static::anchorLink(...$renderer->getPreparedLink($class));
return Blade::render(
'<x-zeus::sky-link :class="$class" :target="$target" :href="$href" :label="$label" />',
$renderer->getPreparedLink($class)
);
}
}
22 changes: 22 additions & 0 deletions src/Components/SkyLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace LaraZeus\Sky\Components;

use Illuminate\View\Component;

class SkyLink extends Component
{

public function __construct(
public string $label,
public ?bool $hasLabelWrap = false,
) {}

/**
* @inheritDoc
*/
public function render()
{
return view('zeus.sky-link', $this->data());
}
}
2 changes: 2 additions & 0 deletions src/SkyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Filament\Facades\Filament;
use Filament\Forms\Components\Select;
use LaraZeus\Core\CoreServiceProvider;
use LaraZeus\Sky\Components\SkyLink;
use LaraZeus\Sky\Console\InstallCommand;
use LaraZeus\Sky\Console\migrateCommand;
use LaraZeus\Sky\Console\PublishCommand;
Expand All @@ -31,6 +32,7 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasCommands($this->getCommands())
->hasViews('zeus')
->hasViewComponent('zeus', SkyLink::class)
->hasRoute('web');
}

Expand Down

0 comments on commit 6b514b4

Please sign in to comment.