التنقل
stepper
يعرض التقدم عبر مسار متعدد الخطوات: الخطوات المكتملة والحالية والقادمة والفاشلة، أفقيًا أو عموديًا.
- الحساب (مكتملة) بياناتك
- الشحن (مكتملة) عنوان التوصيل
-
الدفع (الخطوة الحالية) بطاقة أو تحويل
-
التأكيد (قادمة) المراجعة وإتمام الطلب
-
الرفع (مكتملة)
-
التحقق (خطأ)
-
الاستيراد (قادمة)
-
تم الطلب (مكتملة)
-
تم التغليف (مكتملة)
-
تم الشحن (مكتملة)
-
تم التسليم (مكتملة)
@php
// Normally comes from the controller, e.g. the checkout step stored in the session.
$steps = [
['title' => 'الحساب', 'description' => 'بياناتك', 'href' => '#account'],
['title' => 'الشحن', 'description' => 'عنوان التوصيل', 'href' => '#shipping'],
['title' => 'الدفع', 'description' => 'بطاقة أو تحويل'],
['title' => 'التأكيد', 'description' => 'المراجعة وإتمام الطلب'],
];
$labels = ['complete' => 'مكتملة', 'current' => 'الخطوة الحالية', 'upcoming' => 'قادمة', 'error' => 'خطأ'];
@endphp
<div class="flex w-full max-w-2xl flex-col gap-10">
<x-ui.stepper :steps="$steps" :current="3" label="التقدم" :labels="$labels" />
<div class="grid gap-10 sm:grid-cols-2">
<x-ui.stepper :steps="['الرفع', 'التحقق', 'الاستيراد']" :current="2" :error="2"
orientation="vertical" label="التقدم" :labels="$labels" />
<x-ui.stepper :steps="['تم الطلب', 'تم التغليف', 'تم الشحن', 'تم التسليم']" :current="5"
orientation="vertical" label="التقدم" :labels="$labels" />
</div>
</div>
التثبيت
php artisan ui:add stepper
1. ثبّت الاعتماديات
- composer:
gehrisandro/tailwind-merge-laravel
2.
انسخ الكود إلى
resources/views/components/ui/
resources/views/components/ui/stepper.blade.php
@props([
// Strings, or ['title' => ..., 'description' => ..., 'href' => ...].
'steps' => [],
// 1-based index of the active step. Past the last step = all complete.
'current' => 1,
'orientation' => 'horizontal',
// 1-based index of a step that failed, or null.
'error' => null,
'label' => 'Progress',
'labels' => [],
])
@php
$labels = array_merge(
[
'complete' => 'Completed',
'current' => 'Current',
'upcoming' => 'Upcoming',
'error' => 'Error',
],
$labels,
);
$vertical = $orientation === 'vertical';
$classes = \TailwindMerge\Laravel\Facades\TailwindMerge::merge(
$vertical ? 'flex flex-col' : 'flex w-full items-start',
$attributes->get('class'),
);
$items = [];
foreach (array_values($steps) as $index => $step) {
$number = $index + 1;
$step = is_array($step) ? $step : ['title' => $step];
$state = match (true) {
$error !== null && (int) $error === $number => 'error',
$number < (int) $current => 'complete',
$number === (int) $current => 'current',
default => 'upcoming',
};
$items[] = [
'number' => $number,
'title' => $step['title'] ?? '',
'description' => $step['description'] ?? null,
// Only finished steps link back; the user cannot skip ahead.
'href' => $state === 'complete' ? $step['href'] ?? null : null,
'state' => $state,
'last' => $number === count($steps),
'connector' => $number < (int) $current ? 'complete' : 'upcoming',
];
}
@endphp
<ol aria-label="{{ $label }}" data-orientation="{{ $orientation }}"
{{ $attributes->except('class')->merge(['class' => $classes]) }}>
@foreach ($items as $item)
@php
$indicator = match ($item['state']) {
'complete' => 'bg-primary text-primary-foreground border-primary',
'current' => 'border-primary bg-background text-primary ring-4 ring-primary/20',
'error' => 'bg-destructive text-destructive-foreground border-destructive',
default => 'border-input bg-background text-muted-foreground',
};
$title = match ($item['state']) {
'upcoming' => 'text-muted-foreground',
'error' => 'text-destructive',
default => 'text-foreground',
};
$connector = match ($item['connector']) {
'complete' => 'bg-primary',
default => 'bg-border',
};
$tag = $item['href'] ? 'a' : 'div';
@endphp
<li data-state="{{ $item['state'] }}"
@if ($item['state'] === 'current') aria-current="step" @endif
class="{{ $vertical ? 'relative flex gap-3 pb-8 last:pb-0' : 'relative flex flex-1 flex-col items-center px-2 text-center' }}">
@unless ($item['last'])
{{-- Logical start/end, so the line runs the right way in RTL. --}}
<span aria-hidden="true"
class="{{ $vertical ? 'absolute top-10 bottom-2 start-[calc(1rem-0.5px)] w-px' : 'absolute top-4 start-[calc(50%+1.5rem)] end-[calc(-50%+1.5rem)] h-px' }} {{ $connector }}"></span>
@endunless
<{{ $tag }} @if ($item['href']) href="{{ $item['href'] }}" @endif
class="{{ $vertical ? 'flex gap-3' : 'flex flex-col items-center gap-2' }} rounded-md outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 {{ $item['href'] ? 'group/step' : '' }}">
<span aria-hidden="true"
class="relative z-10 flex size-8 shrink-0 items-center justify-center rounded-full border-2 text-sm font-medium transition-colors {{ $indicator }}">
@if ($item['state'] === 'complete')
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="3"
stroke-linecap="round" stroke-linejoin="round"
class="size-4">
<path d="M20 6 9 17l-5-5" />
</svg>
@elseif ($item['state'] === 'error')
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="3"
stroke-linecap="round" stroke-linejoin="round"
class="size-4">
<path d="M18 6 6 18" />
<path d="m6 6 12 12" />
</svg>
@else
{{ $item['number'] }}
@endif
</span>
<span class="{{ $vertical ? 'flex flex-col pt-1' : 'flex flex-col' }}">
<span
class="text-sm font-medium {{ $title }} group-hover/step:underline">
{{ $item['title'] }}
<span class="sr-only">({{ $labels[$item['state']] }})</span>
</span>
@if ($item['description'])
<span
class="text-xs text-muted-foreground">{{ $item['description'] }}</span>
@endif
</span>
</{{ $tag }}>
</li>
@endforeach
</ol>
الاستخدام
<x-ui.stepper :steps="['Account', 'Billing', 'Confirm']" :current="2" />
{{-- With descriptions, links back to finished steps, vertical. --}}
<x-ui.stepper orientation="vertical" :current="$step" :steps="[
['title' => 'Account', 'description' => 'Your details', 'href' => route('signup', ['step' => 1])],
['title' => 'Billing', 'description' => 'Plan and card', 'href' => route('signup', ['step' => 2])],
['title' => 'Confirm', 'description' => 'Review and finish'],
]" />
{{-- A failed step, plus translated screen-reader labels. --}}
<x-ui.stepper :steps="['Upload', 'Validate', 'Import']" :current="2" :error="2"
:labels="['complete' => 'مكتمل', 'current' => 'الحالي', 'upcoming' => 'قادم', 'error' => 'خطأ']" />
{{-- The controller owns the step. No JavaScript involved:
public function show(Request $request)
{
$step = min(max((int) $request->query('step', 1), 1), 3);
return view('signup', ['step' => $step]);
}
public function store(Request $request)
{
// validate the current step, then move on
return redirect()->route('signup', ['step' => $request->integer('step') + 1]);
}
--}}
ترميز خالص بلا JavaScript: الخادم يحدد current (يبدأ من 1، وأي قيمة بعد الخطوة الأخيرة تجعل كل الخطوات مكتملة). تقبل steps نصوصًا أو مصفوفات ['title', 'description', 'href']، ولا يصبح href رابطًا إلا للخطوات المكتملة. قيمة orientation هي horizontal أو vertical، وerror يحدد خطوة فاشلة، وlabels تستبدل كلمات الحالة لقارئات الشاشة (complete وcurrent وupcoming وerror).
الخصائص
كل خاصية مع قيمتها الافتراضية. مرّر أيًّا منها كسمة، واستخدم : لربط قيمة PHP.
@props([
// Strings, or ['title' => ..., 'description' => ..., 'href' => ...].
'steps' => [],
// 1-based index of the active step. Past the last step = all complete.
'current' => 1,
'orientation' => 'horizontal',
// 1-based index of a step that failed, or null.
'error' => null,
'label' => 'Progress',
'labels' => [],
])