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

how to use in svelte5? #113

Closed
Neptunium1129 opened this issue Jun 1, 2024 · 8 comments
Closed

how to use in svelte5? #113

Neptunium1129 opened this issue Jun 1, 2024 · 8 comments

Comments

@Neptunium1129
Copy link

Neptunium1129 commented Jun 1, 2024

https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes

image

App.svelte

  import { writable } from 'svelte/store';
	import { mount } from 'svelte';
  import Modal, { bind } from 'svelte-simple-modal';
  import Popup from './Popup.svelte';
  const modal = writable(null);

  const showModal = () => modal.set(bind(Popup, { message: "It's a modal!" }));
</script>

<Modal show={$modal}>
  <button onclick={showModal}>Show modal</button>
</Modal>

Popup.svelte

<script>
  export let message = 'Hi';
</script>

<p>🎉 {message} 🍾</p>
@flekschas
Copy link
Owner

The library is not yet tested in svelte v5. Looks like they introduced a breaking change in v5. Please go ahead and propose an ideally backwards compatible fix if you're free.

@Neptunium1129
Copy link
Author

Neptunium1129 commented Jun 4, 2024

The library is not yet tested in svelte v5. Looks like they introduced a breaking change in v5. Please go ahead and propose an ideally backwards compatible fix if you're free.

-- sveltejs/svelte#11472

I tried to change a "new component" to use Svelte 5 Mount command , but I failed. There are a lot of changes.

I'm thinking of leaving svelte...

@dummdidumm
Copy link

From https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes :

If this component is not under your control, you can use the legacy.componentApi compiler option for auto-applied backwards compatibility (note that this adds a bit of overhead to each component).

In other words add a svelte.config.js with

export default {
  compilerOptions: {
    legacy: { componentApi: true }
  }
}

This makes it backwards compatible.

@Neptunium1129
Copy link
Author

From https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes :

If this component is not under your control, you can use the legacy.componentApi compiler option for auto-applied backwards compatibility (note that this adds a bit of overhead to each component).

In other words add a svelte.config.js with

export default {
  compilerOptions: {
    legacy: { componentApi: true }
  }
}

This makes it backwards compatible.

image

after legacy setting

image

@Neptunium1129
Copy link
Author

Neptunium1129 commented Jun 5, 2024

change bind function source

image

export function bind(Component, props = {}) {
      return function ModalComponent(options) {
        /* let app = new Component({
        ...options,
        props: {
          ...props,
          ...options.props,
        },
        }) */
        let cmpData = {
          ...options,
          props: {
            ...props,
            ...options.props,
          },
          target: document.body
        };

        let component = svelte.mount(Component, cmpData);
        debugger;
        return component;
      };
    }

image

  • modal work! but content missing..

i think ctx object missing on mount function

svelte4
image

svelte 5
image

@Neptunium1129
Copy link
Author

Neptunium1129 commented Jun 5, 2024

edit source same.

svelte.config.js

const config = {
	// Consult https://kit.svelte.dev/docs/integrations#preprocessors
	// for more information about preprocessors
	preprocess: vitePreprocess(),

	kit: {
		// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
		// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
		// See https://kit.svelte.dev/docs/adapters for more information about adapters.
		adapter: adapter()
	},
	 compilerOptions: {
		legacy: { componentApi: true }
	} 
};

image

image

@Neptunium1129
Copy link
Author

change new Component code

-- svelte4

return new Component({
           ...options,
           props: {
             ...props,
             ...options.props,
           },
         });

-- svelte5

return mount(Component,{
           ...options,
           props: {
             ...props,
             ...options.props,
           },
           target : options.parentNode  
         });

image

@flekschas
Copy link
Owner

Cool thanks! I'll wait to cutting a new release until Svelte v5 is released. Hopefully there's a backward compatible way to support Svelte v4 and v5.

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

No branches or pull requests

3 participants