flekschas / svelte-simple-modal

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

Cannot respond to real-time theme color changes #104

Open guxuerui opened 1 year ago

guxuerui commented 1 year ago

When I try to make the modal background color change with the theme color, it always fails to update in real-time unless I refresh the page. What should I do?

My code:

let modalBg = 'rgba(255, 255, 255, 1)'
$: if ($currentTheme) {
  // currentTheme is a writable value, and I can get the right value after I change theme
  console.log('[src/routes/+layout.svelte:12] $currentTheme: ', $currentTheme)
  modalBg = $currentTheme === 'dark' ? 'rgba(31, 41, 55, 1)' : 'rgba(255, 255, 255, 1)'
  console.log('[src/routes/+layout.svelte:14] modalBg: ', modalBg)
}
<Modal
  closeButton={true}
  unstyled={false}
  styleWindow={{ width: "80%", backgroundColor: modalBg }}
>
  <slot />
</Modal>

Can anyone provide me with some assistance? Thank you very much.

flekschas commented 8 months ago

You might need to recreate the style object to have its reference change when modalBg changes. E.g.:

$: styleWindow = { width: "80%", backgroundColor: modalBg };

and assign styleWindow to the modal's styleWindow: <Modal styleWindow={styleWindow}>...</Modal>

guxuerui commented 8 months ago

You might need to recreate the style object to have its reference change when modalBg changes. E.g.:

$: styleWindow = { width: "80%", backgroundColor: modalBg };

and assign styleWindow to the modal's styleWindow: <Modal styleWindow={styleWindow}>...</Modal>

OK,thanks for your reply~ I will try this again.