<scriptlang="ts">import type { PageData }from'./$types';import type { Product }from'$lib/types';import{ page }from'$app/stores';import{ browser }from'$app/environment';import ProductSelect from'./ProductSelect.svelte';import Comparison from'./Comparison.svelte';exportletdata: PageData;$: products = data.products;// populate items from query params, when availablelet selectedId1 = $page.url.searchParams.get('item1');let selectedId2 = $page.url.searchParams.get('item2');letitem1: Product |undefined= data.products.find((p)=> p.id.toString()=== selectedId1),item2: Product |undefined= data.products.find((p)=> p.id.toString()=== selectedId2);</script><h1>Select items to compare</h1><formclass="flow"on:submit|preventDefault><ProductSelectid="i1"{products}bind:value={item1}name="item1"><svelte:fragmentslot="label">Item 1</svelte:fragment></ProductSelect><ProductSelectid="i2"{products}bind:value={item2}name="item2"><svelte:fragmentslot="label">Item 2</svelte:fragment></ProductSelect>{#if!browser}<!-- For progressive enhancement, the form does not submit on enter when focusing a select
So we need an actual submit input. Hide it when JS is enabled (which unfortunately causes a layout shift)--><inputtype="submit"/>{/if}</form>{#if item1 && item2}{#if item1.id === item2.id}<p>These are the same items</p>{:else}<Comparison{item1}{item2}/>{/if}{/if}<style>input{display: block;}</style>
<scriptlang="ts">import type { Product }from'$lib/types';exportletitem1: Product;exportletitem2: Product;letcheap: Product,expensive: Product;$:{if(item1.price < item2.price){
cheap = item1;
expensive = item2;}else{
cheap = item2;
expensive = item1;}}$: multiplier = Math.floor(expensive.price / cheap.price);</script><p>
You can get <strong>{multiplier}x</strong><em>{cheap.title}</em> for about the same price as a
single
<em>{expensive.title}</em></p>
<scriptlang="ts">import type { Product }from'$lib/types';exportletid: string;exportletproducts: Product[];exportletvalue: Product |undefined=undefined;exportletname: string;letselectedId: number = value ? value.id :-1;// sync Product with the selected id// we bind to the ID since we need to use it like an actual form$: value = products.find((p)=> p.id === selectedId);</script><labelfor={id}><slotname="label"/></label><!-- Needed autocomplete off, otherwise Firefox would mess with the selected option
https://stackoverflow.com/questions/4831848/firefox-ignores-option-selected-selected --><select{id}bind:value={selectedId}{name}autocomplete="off"><optiondisabledvalue={-1}selected={selectedId ===-1}>Select an item</option>{#eachproducts asp}<!-- Need the actual selected attribute for SSR --><optionvalue={p.id}selected={p.id === selectedId}>{p.title} - ${p.price}</option>{/each}</select><style>label{display: block;}select{--flow-space: 0;padding: 0.5rem;border: 2px solid var(--gray-7);}</style>