-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #10: WIP: Committing sample tab components (and some custom ele…
…ment tag definitions) to facilitate testing of context.
- Loading branch information
1 parent
10c7c03
commit adaefd7
Showing
9 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<script> | ||
import { TabsWrapper, TabList, TabPanel, TabButton } from './tabs'; | ||
</script> | ||
|
||
<TabsWrapper> | ||
<TabList> | ||
<TabButton>one</TabButton> | ||
<TabButton>two</TabButton> | ||
<TabButton>three</TabButton> | ||
</TabList> | ||
|
||
<TabPanel> | ||
<TabsWrapper> | ||
<TabList> | ||
<TabButton>nested one</TabButton> | ||
<TabButton>nested two</TabButton> | ||
<TabButton>nested three</TabButton> | ||
</TabList> | ||
|
||
<TabPanel> | ||
<h2>First nested panel</h2> | ||
</TabPanel> | ||
|
||
<TabPanel> | ||
<h2>Second nested panel</h2> | ||
</TabPanel> | ||
|
||
<TabPanel> | ||
<h2>Third nested panel</h2> | ||
</TabPanel> | ||
</TabsWrapper> | ||
</TabPanel> | ||
|
||
<TabPanel> | ||
<h2>Second panel</h2> | ||
</TabPanel> | ||
|
||
<TabPanel> | ||
<h2>Third panel</h2> | ||
</TabPanel> | ||
</TabsWrapper> | ||
|
||
|
||
<style> | ||
:global(body > *) { | ||
width: 600px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<script> | ||
import { getContext } from 'svelte'; | ||
import { TABS } from './TabsWrapper.svelte'; | ||
const tab = {}; | ||
const { registerTab, selectTab, selectedTab } = getContext(TABS); | ||
registerTab(tab); | ||
</script> | ||
|
||
<style> | ||
button { | ||
background: none; | ||
border: none; | ||
border-bottom: 2px solid white; | ||
border-radius: 0; | ||
margin: 0; | ||
color: #ccc; | ||
} | ||
.selected { | ||
border-bottom: 2px solid teal; | ||
color: #333; | ||
} | ||
</style> | ||
|
||
<button class:selected="{$selectedTab === tab}" on:click="{() => selectTab(tab)}"> | ||
<slot></slot> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div class="tab-list"> | ||
<slot></slot> | ||
</div> | ||
|
||
<style> | ||
.tab-list { | ||
border-bottom: 1px solid teal; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<script> | ||
import { getContext } from 'svelte'; | ||
import { TABS } from './TabsWrapper.svelte'; | ||
const panel = {}; | ||
const testContext = getContext(TABS); | ||
const { registerPanel, selectedPanel } = testContext; | ||
registerPanel(panel); | ||
$: active = ($selectedPanel === panel); | ||
</script> | ||
|
||
<div class:active> | ||
<slot/> | ||
</div> | ||
|
||
|
||
<style> | ||
div { | ||
display: none; | ||
} | ||
div.active{ | ||
display: block; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { default as TabsWrapper } from './TabsWrapper.svelte'; | ||
export { default as TabList } from './TabList.svelte'; | ||
export { default as TabPanel } from './TabPanel.svelte'; | ||
export { default as TabButton } from './TabButton.svelte'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<link rel="icon" type="image/svg+xml" href="/svelte-retag-favicon.svg"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<title>svelte-retag demo (tabs demo)</title> | ||
<script type="module" src="src/main.js"></script> | ||
</head> | ||
<body> | ||
|
||
<div id="app"> | ||
<h1>Tabs demo</h1> | ||
<!-- TODO: Write up explanation of what is happening below. --> | ||
<p>Thanks to | ||
<a href="https://stackoverflow.com/users/1990514/amir-pournasserian" target="_blank" rel="noopener">AmirPournasserian</a> | ||
from | ||
<a href="https://stackoverflow.com/questions/75024281/how-to-avoid-overwriting-context-in-nested-components-like-tabs-in-svelte" target="_blank" rel="noopener"> | ||
this StackOverflow post</a> for the tab components which rely on this context.</p> | ||
|
||
<!-- TODO: All contents below are a WIP during debugging --> | ||
|
||
<tabs-demo></tabs-demo> | ||
|
||
|
||
<!-- Simple --> | ||
<!--<tabs-wrapper> | ||
<tab-list> | ||
<tab-button>one</tab-button> | ||
<tab-button>two</tab-button> | ||
<tab-button>three</tab-button> | ||
</tab-list> | ||
<tab-panel> | ||
<h2>First panel</h2> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Second panel</h2> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Third panel</h2> | ||
</tab-panel> | ||
</tabs-wrapper>--> | ||
|
||
<!-- Simple nested: Top level context --> | ||
<!--<tabs-wrapper> | ||
<tab-list> | ||
<tab-button>one</tab-button> | ||
<tab-button>two</tab-button> | ||
</tab-list> | ||
<tab-panel> | ||
<h2>First panel</h2> | ||
<tabs-wrapper> | ||
<tab-list> | ||
<tab-button>Nested one</tab-button> | ||
<tab-button>Nested two</tab-button> | ||
</tab-list> | ||
<tab-panel> | ||
<h2>Nested first panel</h2> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Nested second panel</h2> | ||
</tab-panel> | ||
</tabs-wrapper> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Second panel</h2> | ||
</tab-panel> | ||
</tabs-wrapper>--> | ||
|
||
|
||
<!-- Complex nested: Top level context --> | ||
<!--<tabs-wrapper> | ||
<tab-list> | ||
<tab-button>one</tab-button> | ||
<tab-button>two</tab-button> | ||
<tab-button>three</tab-button> | ||
</tab-list> | ||
<tab-panel> | ||
fails when nesting | ||
<!– Has nested context –> | ||
<tabs-wrapper> | ||
<tab-list> | ||
<tab-button>nested one</tab-button> | ||
<tab-button>nested two</tab-button> | ||
</tab-list> | ||
<tab-panel> | ||
<h2>First nested panel</h2> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Second nested panel</h2> | ||
</tab-panel> | ||
</tabs-wrapper> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Second panel</h2> | ||
</tab-panel> | ||
<tab-panel> | ||
<h2>Third panel</h2> | ||
</tab-panel> | ||
</tabs-wrapper>--> | ||
|
||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters