Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Markdoc tags - Tabs, YT embed #35

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/components/mdoc/Tabs/Tab.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
import { getContext } from 'svelte';
import { TABS } from './Tabs.svelte';

const tab = {};
const { registerTab, selectTab, selectedTab } = getContext(TABS);

registerTab(tab);
</script>

<style>
button {
@apply bg-none border-b-2 border-solid border-white m-0 text-gray-400 px-4 py-1;
}

.selected {
@apply border-b-2 border-solid border-gray-700 text-gray-700;
}
</style>

<button class:selected="{$selectedTab === tab}" on:click="{() => selectTab(tab)}">
<slot></slot>
</button>
9 changes: 9 additions & 0 deletions src/components/mdoc/Tabs/TabList.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="tab-list">
<slot></slot>
</div>

<style>
.tab-list {
@apply border-b border-solid border-gray-500;
}
</style>
13 changes: 13 additions & 0 deletions src/components/mdoc/Tabs/TabPanel.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { getContext } from 'svelte';
import { TABS } from './Tabs.svelte';

const panel = {};
const { registerPanel, selectedPanel } = getContext(TABS);

registerPanel(panel);
</script>

{#if $selectedPanel === panel}
<slot></slot>
{/if}
11 changes: 11 additions & 0 deletions src/components/mdoc/Tabs/Tabs.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Tabs from './index.svelte'
import type { TabItem } from './tabs'

interface Props {
tabs: TabItem[]
}

const { tabs } = Astro.props
---
<Tabs tabs={tabs} client:visible/>
50 changes: 50 additions & 0 deletions src/components/mdoc/Tabs/Tabs.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<script context="module">
export const TABS = {};
</script>

<script>
import { setContext, onDestroy } from 'svelte';
import { writable } from 'svelte/store';

const tabs = [];
const panels = [];
const selectedTab = writable(null);
const selectedPanel = writable(null);

setContext(TABS, {
registerTab: tab => {
tabs.push(tab);
selectedTab.update(current => current || tab);

onDestroy(() => {
const i = tabs.indexOf(tab);
tabs.splice(i, 1);
selectedTab.update(current => current === tab ? (tabs[i] || tabs[tabs.length - 1]) : current);
});
},

registerPanel: panel => {
panels.push(panel);
selectedPanel.update(current => current || panel);

onDestroy(() => {
const i = panels.indexOf(panel);
panels.splice(i, 1);
selectedPanel.update(current => current === panel ? (panels[i] || panels[panels.length - 1]) : current);
});
},

selectTab: tab => {
const i = tabs.indexOf(tab);
selectedTab.set(tab);
selectedPanel.set(panels[i]);
},

selectedTab,
selectedPanel
});
</script>

<div class="tabs">
<slot></slot>
</div>
24 changes: 24 additions & 0 deletions src/components/mdoc/Tabs/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script lang="ts">
import { Tabs, TabList, TabPanel, Tab, type TabItem } from './tabs';
export let tabs: TabItem[] = []
</script>

<Tabs>
<TabList>
{#each tabs as tab}
<Tab>{tab.title}</Tab>
{/each}
</TabList>

{#each tabs as tab}
<TabPanel>
<div class="body">{tab.body}</div>
</TabPanel>
{/each}
</Tabs>

<style>
.body {
@apply px-4 py-1;
}
</style>
9 changes: 9 additions & 0 deletions src/components/mdoc/Tabs/tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { default as Tabs } from './Tabs.svelte';
export { default as TabList } from './TabList.svelte';
export { default as TabPanel } from './TabPanel.svelte';
export { default as Tab } from './Tab.svelte';

export interface TabItem {
title: string
body: string
}
4 changes: 2 additions & 2 deletions src/components/mdoc/TweetEmbed.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const { url } = Astro.props
<div
class="twitter-embed flex flex-col items-center justify-center relative"
>
<div
<blockquote
class="twitter-tweet"
data-conversation="none"
data-theme="light"
data-lang="en"
data-dnt="true"
>
<a class="unset no-underline text-current absolute top-0 left-0" href={url}>Loading embedded tweet...</a>
</div>
</blockquote>
</div>

<script
Expand Down
28 changes: 28 additions & 0 deletions src/components/mdoc/YTVideoEmbed.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
interface Props {
title: string
url: string
}

const { url, title } = Astro.props;
---

<div>
<iframe
class="yt-iframe"
width="560"
height="315"
src={url}
title={title}
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowfullscreen
>
</iframe>
</div>

<style>
.yt-iframe {
@apply w-full aspect-[16/9]
}
</style>
6 changes: 5 additions & 1 deletion src/utils/mdoc/mdoc.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import type { Config } from '@markdoc/markdoc';
import { callout } from './schema/callout.mdoc';
import { link } from './schema/link.mdoc';
import { tweetEmbed } from './schema/tweet-embed.mdoc';
import { tabs } from './schema/tabs.mdoc';
import { ytEmbed } from './schema/yt-embed.mdoc';
// import { heading } from './schema/heading.mdoc';

export const config: Config = {
tags: {
callout,
link,
tweet: tweetEmbed
tweet: tweetEmbed,
yt: ytEmbed,
tabs
},
functions: {
getCountryEmoji: {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/mdoc/schema/tabs.mdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Schema } from '@markdoc/markdoc';

export const tabs: Schema = {
render: 'Tabs',
children: ['paragraph', 'tag', 'list'],
attributes: {
tabs: {
type: Array,
},
heading: {
type: String
}
}
};
15 changes: 15 additions & 0 deletions src/utils/mdoc/schema/yt-embed.mdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Schema } from '@markdoc/markdoc';

export const ytEmbed: Schema = {
render: 'YTVideoEmbed',
attributes: {
url: {
type: String,
required: true
},
title: {
type: String,
required: true
}
}
};