Skip to content

Commit

Permalink
add 'Open in StackBlitz' links to example code fences
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Dec 27, 2022
1 parent 52dea35 commit ac07557
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 36 deletions.
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Any new features should come with corresponding tests. If you fix a bug, please
This repo has 3 required CI checks that have to pass for every PR before merging:

- tests: run as [GitHub Action](https://github.com/janosh/svelte-multiselect/actions/workflows/test.yml) ([workflow code](https://github.com/janosh/svelte-multiselect/blob/main/.github/workflows/test.yml))
- linting: handled by [pre-commit.ci](https://results.pre-commit.ci/repo/github/365228700)
- linting: handled by [pre-commit.ci](https://results.pre-commit.ci/latest/github/janosh/svelte-multiselect/main)
- docs: [continuous deployment](https://github.com/janosh/svelte-multiselect/blob/main/.github/workflows/gh-pages.yml) through GitHub Pages

## 🆕 New release
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": "vitest --run --coverage tests/unit/*.ts && playwright test tests/*.test.ts",
"test:unit": "vitest tests/unit/*.ts",
"test:e2e": "playwright test tests/*.test.ts",
"changelog": "npx auto-changelog -p --output changelog.md --unreleased-only --hide-credit --commit-limit false"
"changelog": "npx auto-changelog --package --output changelog.md --unreleased-only --hide-credit --commit-limit false"
},
"devDependencies": {
"@iconify/svelte": "^3.0.1",
Expand Down
13 changes: 10 additions & 3 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ main {
width: 100%;
max-width: 50em;
}
button {
color: var(--text-color);
button,
a.btn {
color: white;
cursor: pointer;
border: none;
border-radius: 3pt;
background: darkcyan;
background-color: darkcyan;
padding: 2pt 4pt;
font-size: 12pt;
line-height: initial;
transition: background-color 0.2s;
}
:is(button, a.btn):hover {
background-color: teal;
color: white;
}
a {
color: var(--blue);
Expand Down
2 changes: 1 addition & 1 deletion src/components/CollapseIcon.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- https://icon-sets.iconify.design/bx/collapse-vertical -->
<svg width="32" height="32" viewBox="0 0 24 24" fill="currentColor" {...$$props}>
<svg width="32" height="32" viewBox="0 0 24 24" fill="currentColor" {...$$props} on:click>
<path
d="M12 7.59L7.05 2.64L5.64 4.05L12 10.41l6.36-6.36l-1.41-1.41L12 7.59zM5.64 19.95l1.41 1.41L12 16.41l4.95 4.95l1.41-1.41L12 13.59l-6.36 6.36z"
/>
Expand Down
45 changes: 27 additions & 18 deletions src/components/ExampleCode.svelte
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
<script lang="ts">
import { page } from '$app/stores'
import Icon from '@iconify/svelte'
import { onMount } from 'svelte'
import { name } from '../../package.json'
import CopyButton from './CopyButton.svelte'
export let src: string // just here to avoid unknown prop warning
export let meta: Record<string, string>
let node: HTMLElement
export let src: string // just here to avoid unknown prop warning
src
$: ({ id } = meta)
let node: HTMLElement
onMount(() => {
// replace $lib with 'name' in code
node.innerHTML = node.innerHTML.replace(/\$lib/g, name)
// replace $lib with package name in code
node.innerHTML = node.innerHTML.replaceAll(`$lib`, name)
})
$: stackblitz_url = `https://stackblitz.com/github/janosh/svelte-multiselect?file=${encodeURIComponent(
`src/routes${$page.route.id}/+page.svx`
)}`
</script>

{#if id}
<div {id}>
<slot name="example" />
</div>
{:else}
<div id={meta.id}>
<slot name="example" />
{/if}

<pre><aside>
<CopyButton content={node?.innerText} />
</aside><code bind:this={node}><slot name="code" /></code></pre>
<div>
<aside>
<CopyButton content={node?.innerText} />
<a href={stackblitz_url} class="btn" target="_blank" rel="noreferrer">
<Icon icon="simple-icons:stackblitz" inline />
Open in StackBlitz
</a>
</aside>
<pre><code bind:this={node}><slot name="code" /></code></pre>
</div>
</div>

<style>
div {
position: relative;
}
pre {
margin-top: 2em;
background-color: rgba(255, 255, 255, 0.05);
position: relative;
}
aside {
position: absolute;
top: 1em;
right: 1em;
display: flex;
gap: 1em;
gap: 1ex;
place-items: center;
}
</style>
8 changes: 4 additions & 4 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
import { _demo_routes } from './+layout'
$: is_current = (path: string) => {
if (path === $page.url.pathname) return `page`
if (path !== `/` && $page.url.pathname.includes(path)) return `page`
if (path === $page.route.id) return `page`
if (path !== `/` && $page.route.id.includes(path)) return `page`
return undefined
}
</script>

<GitHubCorner href={repository} />

{#if !$page.error && $page.url.pathname !== `/`}
{#if !$page.error && $page.route.id !== `/`}
<a href="." aria-label="Back to index page">&laquo; home</a>
{/if}
{#if _demo_routes.some((route) => $page.url.pathname.endsWith(route))}
{#if _demo_routes.some((route) => $page.route.id.endsWith(route))}
<h1>
<img src="favicon.svg" alt="Svelte MultiSelect" height="50" width="50" />&ensp;Svelte
MultiSelect Examples
Expand Down
2 changes: 1 addition & 1 deletion src/routes/disabled/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</script>

<MultiSelect
options={[{ label: `Svelte`, value: `Svelte`, preselected: true }]}
options={[{ label: `Svelte`, preselected: true }]}
disabled
disabledInputTitle="Super special disabled message"
--sms-disabled-bg="darkred"
Expand Down
4 changes: 2 additions & 2 deletions src/routes/kit-form-actions/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import hljs from 'highlight.js/lib/common'
import 'highlight.js/styles/vs2015.css'

// const glob = import.meta.glob('./*', { eager: true, as: 'raw' })
// const server_code = glob['./+page.server.ts']
// const glob = import.meta.glob(`./*`, { eager: true, as: `raw` })
// const server_code = glob[`./+page.server.ts`]
</script>

## Hook up Multiselect to SvelteKit form action incl. form validation
Expand Down
4 changes: 2 additions & 2 deletions src/routes/persistent/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import hljs from 'highlight.js/lib/common'
import 'highlight.js/styles/vs2015.css'

const glob = import.meta.glob('/src/stores.ts', { eager: true, as: 'raw' })
const store_code = glob['/src/stores.ts']
const glob = import.meta.glob(`/src/stores.ts`, { eager: true, as: `raw` })
const store_code = glob[`/src/stores.ts`]
</script>

## Page-Reload Persistent MultiSelect
Expand Down
17 changes: 15 additions & 2 deletions src/routes/slots/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import MultiSelect from '$lib'
import { languages } from '$src/options'
import LanguageSlot from '$src/components/LanguageSlot.svelte'
import MinusIcon from '$src/components/MinusIcon.svelte'
import ExpandIcon from '$src/components/ExpandIcon.svelte'
import CollapseIcon from '$src/components/CollapseIcon.svelte'
import ExpandIcon from '$src/components/ExpandIcon.svelte'
import MinusIcon from '$src/components/MinusIcon.svelte'
</script>

<MultiSelect
Expand Down Expand Up @@ -38,16 +38,29 @@
import MultiSelect from '$lib'
import { languages } from '$src/options'
import LanguageSlot from '$src/components/LanguageSlot.svelte'
import CollapseIcon from '$src/components/CollapseIcon.svelte'
import ExpandIcon from '$src/components/ExpandIcon.svelte'

// local variable used in CollapseIcon on:click callback to close dropdown
let open
</script>

<MultiSelect
options={languages}
maxSelect={5}
placeholder="What languages do you know?"
selected={['Python', 'TypeScript', 'Julia']}
bind:open
>
<LanguageSlot let:option {option} slot="selected" />
<LanguageSlot let:option {option} slot="option" />
<svelte:fragment slot="expand-icon">
{#if open}
<CollapseIcon width="1em" on:click={() => (open = false)} />
{:else}
<ExpandIcon width="1em" />
{/if}
</svelte:fragment>
<span slot="remove-icon">x</span>
</MultiSelect>
```
4 changes: 3 additions & 1 deletion tests/multiselect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ test.describe(`disabled multiselect`, async () => {
})

test(`renders no buttons`, async ({ page }) => {
expect(await page.$$(`#disabled-input-title button`)).toHaveLength(0)
expect(
await page.$$(`#disabled-input-title div.multiselect button`)
).toHaveLength(0)
})

test(`renders disabled slot`, async ({ page }) => {
Expand Down

0 comments on commit ac07557

Please sign in to comment.