budibase/packages/standard-components/src/Nav.svelte

117 lines
2.6 KiB
Svelte
Raw Normal View History

2019-09-26 06:40:58 +02:00
<script>
2020-02-03 10:50:30 +01:00
import cssVars from "./cssVars"
export let navBarBackground = ""
export let navBarBorder = ""
export let navBarColor = ""
export let selectedItemBackground = ""
export let selectedItemColor = ""
export let selectedItemBorder = ""
export let itemHoverBackground = ""
export let itemHoverColor = ""
export let hideNavBar = false
export let selectedItem = ""
export let _children
export let _bb
let selectedIndex = -1
let styleVars = {}
let components = {}
let componentElements = {}
const hasComponentElements = () =>
Object.getOwnPropertyNames(componentElements).length > 0
$: {
2019-10-03 07:12:13 +02:00
styleVars = {
2020-02-03 10:50:30 +01:00
navBarBackground,
navBarBorder,
navBarColor,
selectedItemBackground,
selectedItemColor,
selectedItemBorder,
itemHoverBackground,
itemHoverColor,
}
2019-10-03 07:12:13 +02:00
2020-02-03 10:50:30 +01:00
if (_children && _children.length > 0 && hasComponentElements()) {
const currentSelectedItem =
selectedIndex > 0 ? _children[selectedIndex].title : ""
if (selectedItem && currentSelectedItem !== selectedItem) {
let i = 0
for (let child of _children) {
if (child.title === selectedItem) {
onSelectItem(i)()
}
i++
2019-10-03 07:12:13 +02:00
}
2020-02-03 10:50:30 +01:00
} else if (!currentSelectedItem) {
onSelectItem(0)
}
2019-10-03 07:12:13 +02:00
}
2020-02-03 10:50:30 +01:00
}
const onSelectItem = index => () => {
selectedIndex = index
if (!components[index]) {
2020-02-25 16:21:23 +01:00
const comp = _bb.attachChildren(componentElements[index])
2020-02-03 10:50:30 +01:00
components[index] = comp
2019-10-07 07:03:41 +02:00
}
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
</script>
<div class="root" use:cssVars={styleVars}>
2020-02-03 10:50:30 +01:00
{#if !hideNavBar}
2019-09-26 06:40:58 +02:00
<div class="navbar">
2020-02-03 10:50:30 +01:00
{#each _children as navItem, index}
<div
class="navitem"
on:click={onSelectItem(index)}
class:selected={selectedIndex === index}>
{navItem.title}
2019-09-26 06:40:58 +02:00
</div>
2020-02-03 10:50:30 +01:00
{/each}
2019-09-26 06:40:58 +02:00
</div>
2020-02-03 10:50:30 +01:00
{/if}
{#each _children as navItem, index}
<div class="content" bind:this={componentElements[index]} />
{/each}
2019-09-26 06:40:58 +02:00
</div>
<style>
2020-02-03 10:50:30 +01:00
.root {
2019-09-26 06:40:58 +02:00
height: 100%;
2020-02-03 10:50:30 +01:00
width: 100%;
2019-09-26 06:40:58 +02:00
grid-template-columns: [navbar] auto [content] 1fr;
display: grid;
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
2020-02-03 10:50:30 +01:00
.navbar {
2019-09-26 06:40:58 +02:00
grid-column: navbar;
background: var(--navBarBackground);
border: var(--navBarBorder);
color: var(--navBarColor);
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
2020-02-03 10:50:30 +01:00
.navitem {
2019-09-26 06:40:58 +02:00
padding: 10px 17px;
cursor: pointer;
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
2020-02-03 10:50:30 +01:00
.navitem:hover {
2019-09-26 06:40:58 +02:00
background: var(--itemHoverBackground);
color: var(--itemHoverColor);
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
2020-02-03 10:50:30 +01:00
.navitem.selected {
2019-09-26 06:40:58 +02:00
background: var(--selectedItemBackground);
border: var(--selectedItemBorder);
color: var(--selectedItemColor);
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
2020-02-03 10:50:30 +01:00
.content {
2019-09-26 06:40:58 +02:00
grid-column: content;
2020-02-03 10:50:30 +01:00
}
2019-09-26 06:40:58 +02:00
</style>