diff --git a/src/Modal.php b/src/Modal.php index 5ce7a47..0a7d45a 100644 --- a/src/Modal.php +++ b/src/Modal.php @@ -14,11 +14,13 @@ class Modal implements Responsable { protected string $baseURL; + protected array $props = []; public function __construct( protected string $component, - protected array|Arrayable $props = [] + array|Arrayable $props ) { + $this->with($props); } public function baseRoute(string $name, mixed $parameters = [], bool $absolute = true): static @@ -33,17 +35,21 @@ public function basePageRoute(string $name, mixed $parameters = [], bool $absolu return $this->baseRoute($name, $parameters, $absolute); } - public function with(array $props): static + public function with(array|Arrayable $props): static { - $this->props = $props; + $this->props = $props instanceof Arrayable ? $props->toArray() : $props; return $this; } public function render(): mixed { + $flatProps = []; + foreach ($this->props as $key => $prop) { + $flatProps['modal.props.'.$key] = $prop; + } /** @phpstan-ignore-next-line */ - inertia()->share(['modal' => $this->component()]); + inertia()->share(['modal' => $this->component(), ...$flatProps]); // render background component on first visit if (request()->header('X-Inertia') && request()->header('X-Inertia-Partial-Component')) { @@ -100,8 +106,7 @@ protected function component(): array 'component' => $this->component, 'baseURL' => $this->baseURL, 'redirectURL' => $this->redirectURL(), - 'props' => $this->props, - 'key' => request()->header('X-Inertia-Modal-Key', Str::uuid()->toString()), + 'key' => request()->header('X-Inertia-Modal-Key') ?? Str::uuid()->toString(), 'nonce' => Str::uuid()->toString(), ]; }