Modify parent’s footer slot from child

In Svelte, I am using the Modal component.

This component provides the possibility do declare a div with the property slot="footer".

This will keep the footer always at the bottom of the modal. It will make the body scroll if there is a lot of content, while keeping the footer visible anchored at the bottom of the modal.

My problem?

I’d like to be able to modify the content of this footer from a child component that I am using inside the Modal component. This child component has custom logic that will modify the footer content (not text, but html elements like buttons).

I tried to move the div <div slot="footer"> inside the child component but I got this error:

Element with a slot='...' attribute must be a child of a component or a descendant of a custom element

I also tried this solution but I did not managed to make it work. Not sure if I did an error or it’s not really related to my case (I’m still a new with Svelte).
It seems like that the “footer” option in the child component does NOT reach the parent component.

This is my code:

<script lang="ts">
    import { Modal } from "flowbite-svelte";
    import TestComponent from "./TestComponent.svelte";
</script>
Parent component:

<Modal open={true} size="lg" class="m-4">
    <!-- Modal body -->
    <div class="space-y-6 p-0"><p>BODY CONTENT</p></div>

    <TestComponent />

    <!-- Modal footer -->
    <div slot="footer" class="flex w-full justify-end">
        <p>FOOTER CONTENT</p>
    </div>
</Modal>
<!-- Child component -->
<p>SOME TEXT THAT MUST BE CONFIRMED WITH A BUTTON IN THE MODAL</p>

<!-- show button A or B in parent component based on some logic -->

<!-- <div slot="footer" class="flex w-full justify-end"></div> - enabling triggers error -->

Is there a way to achieve this? Of course I am aware that I could move the logic in the footer directly, but I was trying to modularize the code.

Thanks in advance for any help!

Use events1 or bound properties to communicate between the child component and the parent. E.g.

<script>
  let mode;
</script>

<Model>
  ...
  <Child bind:mode />

  <div slot="footer" class="flex w-full justify-end">
    {#if mode == 'a'}
      <!-- Button A here -->
    {:else if mode == 'b'}
      <!-- Button B here -->
    {/if}
  </div>
</Modal>
<!-- child -->
<script>
  export let mode; // <- change this as needed
</script>

If the buttons should be extracted to their own component, you could pass the property down into said component.

For more indirect communication it is also possible to use a store in a context. For that to work and the same context being shared between relevant components, the context has to be set at a common ancestor in the component hierarchy, in this case e.g. the parent component containing the modal. Both the child and any components in the footer then can update and react to changes in the store.

1 In Svelte 5, events are done via callback functions as props rather than using createEventDispatcher.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật