Witam.
Chciałbym stworzyć globalny kod w projekcie, który będzie odpowiadał za przekazywanie eventu kliknięcia do parenta. Ogólnie buduje sobie własne komponenty, które nie będą bardzo skomplikowane, głównie chodzi, aby ostylować element raz i wykorzystać go gdzie będzie potrzeba. Korzystam z tailwind.
PrimaryIconButton.svelte
- podaje tylko ikonę.
<script>
import { createEventDispatcher } from 'svelte';
export let icon = '';
const dispatch = createEventDispatcher();
function clickHandle() {
dispatch('click');
}
</script>
<button
on:click={clickHandle}
class="bg-primary-500 rounded-md w-14 hover:bg-primary-600 active:bg-primary-700 p-3 flex items-center justify-center"
>
<i class="{icon} text-3xl text-slate-50" />
</button>
PrimaryTextButton.svelte
- podaje tekst oraz typ/kolor
<script>
import { createEventDispatcher } from 'svelte';
export let text = '';
export let type = '';
const dispatch = createEventDispatcher();
function clickHandle() {
dispatch('click');
}
</script>
{#if type === 'primary'}
<button
on:click={clickHandle}
class="bg-primary-500 rounded-md w-32 hover:bg-primary-600 active:bg-primary-700 text-slate-50 p-3 flex items-center justify-center"
>
{text}
</button>
{:else if type === 'warn'}
<button
on:click={clickHandle}
class="bg-red-500 rounded-md w-32 hover:bg-red-600 active:bg-red-700 text-slate-50 p-3 flex items-center justify-center"
>
{text}
</button>
{/if}
Oba te komponenty mają ten sam clickHandle()
, ponieważ logika kliknięcia będzie w parencie.
Potrzebuje tą część
const dispatch = createEventDispatcher();
function clickHandle() {
dispatch('click');
}
zrobić "globalnie", aby nie powielać kodu.
katakrowa