Skip to content

Shared Context for Layouts #5467

@TorstenDittmann

Description

@TorstenDittmann

Describe the problem

In certain use-cases you might have some logic (fetching data, state, whatever) inside a __layout.svelte file. If you now want to share behavior with multiple named layouts on the same "level", you are forced to create a component that you can use in each of those or repeat yourself.

I think especially when somebody is working frequently with very diverse layouts on a project, that can be quite repetitive.

Right now the implementation would look like this:

// +layout.svelte
<script>
    import Context from './context.svelte';
</script>

<Context>
    <div class="side-default">
        <slot />
    </div>
</Context>
// +layout-lorem.svelte
<script>
    import Context from './context.svelte';
</script>

<Context>
    <div class="side-lorem">
        <slot />
    </div>
</Context>
// context.svelte
<script>
    // Do something here for all Layouts
</script>

<slot />

Describe the proposed solution

I could imagine something like a +context.svelte, which acts like a layout - but would act like a parent layout to the +layout.svelte and +layout-lorem.svelte files.

Even if a +layout.svelte is not used on the way when using a named layout, the +context.svelte is still applied.

routes/
├─ home/
│  ├─ info/
│  │  ├─ +page.svelte
│  │  ├─ +layout-lorem.svelte // ../+context.svelte still applies here
│  │  ├─ +layout.svelte
│  ├─ +page.svelte
│  ├─ +layout.svelte
│  ├─ +context.svelte
├─ +page.svelte
├─ +layout.svelte
├─ +layout-lorem.svelte
├─ +context.svelte

I am not super confident in the naming here, but I think the idea is that matters.

// +layout.svelte
<div class="side-default">
    <slot />
</div>
// +layout-lorem.svelte
<div class="side-lorem">
    <slot />
</div>
// +context.svelte
<script>
    // Do something here for all Layouts
</script>

<slot />

Alternatives considered

Though it's a very minor inconvenience, I can see room for improvement.

Importance

would make my life easier

Additional Information

I'd be more than willing to dive deeper into the implementation and draft out a RFC if there is interest 👍🏻

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions