flekschas / svelte-simple-modal

A simple, small, and content-agnostic modal for Svelte v3 and v4
https://svelte.dev/repl/b95ce66b0ef34064a34afc5c0249f313
MIT License
420 stars 30 forks source link

Svelte5 migration #114

Closed Neptunium1129 closed 2 months ago

Neptunium1129 commented 2 months ago

I am attempting to migrate this modal source because it is a Svelte modal, which is the most stable and functional.

The modal open function is working.

However, the "store bind" has failed, and clicking behind the modal causes the component to duplicate and mount again.

-- behind click focus duplicate mount component

<script>
const etcModuleHistListModal = writable(null);
    const etcModuleHistListBtn = () => {
        etcModuleHistListModal.set(bind(ModalContent,
            {
                title: "BIND TITLE"
            }
         )
        )
    };
</script>
<p><button onclick={(e)=>{
        etcModuleHistListBtn()
    }
    }>BIND_BTN</button></p>

image image

Temporary Solution: Adding e.target.blur();

<p><button onclick={(e)=>{
    e.target.blur();
    cmmModalBtn();
}
}>CUSTOM_CMM</button></p>

-- Usage With a Svelte Store Problem

  1. OnMount changed $effect, then isMounted is set to true.
  2. Reactivity issue: Component duplicate bind,
  3. I thought OnMount and reactivity ($:) changed $effect.
  isMounted = $state(false);
$effect(()=>{
  isMounted = true;

  if (isMounted) {
    if (isFunction(show)) {
      open(show);
    } else {

      close();

    }
  }

  return () => {
    if (isMounted) close();
  }
});
<!-- App.svelte -->
<script>
  import { writable } from 'svelte/store';
  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 on:click={showModal}>Show modal</button>
</Modal>
Neptunium1129 commented 2 months ago

https://svelte.dev/repl/52e0ade6d42546d8892faf8528c70d30?version=3.59.1 Honeycam 2024-06-09 12-22-13

https://svelte.dev/repl/52e0ade6d42546d8892faf8528c70d30?version=4.2.18 Honeycam 2024-06-09 12-23-03

The issue of duplicate components occurring on button click happens in version 4.2.18 but does not occur in version 3.59.1.

Neptunium1129 commented 2 months ago
let {
  children,
  show = null,
  id = null,
  key = 'simple-modal',
  isTabbable = baseIsTabbable,
  ariaLabel = null,
  ariaLabelledBy = null,
  closeButton = true,
  closeOnEsc = true,
  closeOnOuterClick = true,
  styleBg = {},
  styleWindow = {},
  styleContent = {},
  styleCloseButton = {},
  styleWindowWrap = {},
  classBg = null,
  classWindowWrap = null,
  classWindow = null,
  classContent = null,
  classCloseButton = null,
  unstyled = false,
  setContext = baseSetContext,
  transitionBg = fade,
  transitionBgProps = { duration: 250 },
  transitionWindow = transitionBg,
  transitionWindowProps = transitionBgProps,
  disableFocusTrap = false
} = $props();

The duplicate component issue is caused by using the fade transition. The issue occurs with a duration of 250, but does not occur with a duration of 120.