Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add @slots directive for optional component slots #47574

Closed
wants to merge 3 commits into from

Conversation

indykoning
Copy link
Contributor

@indykoning indykoning commented Jun 26, 2023

This PR will add a @slots blade directive mainly for use in anonymous components.
It adds a mechanism to declare optional slots at the top of your components which will automatically be transformed into ComponentSlots when they are a string or do not exist.

The issue this solves is, say you're building a dialog component with text and a button which you can optionally change the styling and contents of. You might have your template look like this.

<dialog {{ $attributes->class('absolute inset') }}>
	{{ $slot }}
	<div>
		<button {{ $button->attributes->class('bg-green-200 ml-0 mr-auto') }}>{{ $button ?? 'close' }}</button>
	</div>
</dialog>

However this will never work as button is null and $button->attributes will throw an error let alone merging classes.
The options you have are adding php at the top or coalescing the $button->attributes->class and copy-pasting your classes.

The solution this offers is the following template

@slots(['button' => ['contents' => 'close']])
<dialog {{ $attributes->class('absolute inset') }}>
	{{ $slot }}
	<div>
		<button {{ $button->attributes->class('bg-green-200 ml-0 mr-auto') }}>{{ $button }}</button>
	</div>
</dialog>

Which will define all optional slots used in the code clearly at the top, and allow us to set default data to be overwritten if the named slot is used.

This is not breaking as it is a new feature which affects nothing if it is not used.

@indykoning indykoning changed the title Add @slots directive for optional component slots [10.x] Add @slots directive for optional component slots Jun 26, 2023
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@royduin
Copy link
Contributor

royduin commented Jun 28, 2023

If somebody wants it: https://github.com/rapidez/blade-directives

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants