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

114 lines
2.5 KiB
Svelte
Raw Normal View History

2019-09-26 06:40:58 +02:00
<script>
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="";
2019-10-03 07:12:13 +02:00
export let items = [];
export let hideNavBar=false;
export let selectedItem="";
2019-09-26 06:40:58 +02:00
export let _bb;
2019-09-26 06:40:58 +02:00
2019-10-03 07:12:13 +02:00
let selectedIndex = -1;
2019-09-26 06:40:58 +02:00
let contentElement;
2019-10-03 07:12:13 +02:00
let styleVars={};
let currentComponent;
$: {
styleVars = {
navBarBackground, navBarBorder,
navBarColor, selectedItemBackground,
selectedItemColor, selectedItemBorder,
itemHoverBackground, itemHoverColor
};
if(items && items.length > 0 && contentElement) {
const currentSelectedItem = selectedIndex > 0
? items[selectedIndex].title
: "";
if(selectedItem && currentSelectedItem !== selectedItem) {
let i=0;
for(let item of items) {
if(item.title === selectedItem) {
onSelectItem(i)();
}
i++;
}
} else if(!currentSelectedItem) {
onSelectItem(0);
}
}
2019-09-26 06:40:58 +02:00
}
const onSelectItem = (index) => () => {
selectedIndex = index;
2019-10-03 07:12:13 +02:00
if(currentComponent) currentComponent.$destoy();
currentComponent = _bb.initialiseComponent(items[index].component, contentElement);
2019-09-26 06:40:58 +02:00
}
</script>
<div class="root" use:cssVars={styleVars}>
2019-10-03 07:12:13 +02:00
{#if !hideNavBar}
2019-09-26 06:40:58 +02:00
<div class="navbar">
{#each items as navItem, index}
<div class="navitem"
on:click={onSelectItem(index)}
class:selected={selectedIndex === index}>
{navItem.title}
</div>
{/each}
</div>
2019-10-03 07:12:13 +02:00
{/if}
2019-09-26 06:40:58 +02:00
<div class="content"
bind:this={contentElement}>
</div>
</div>
<style>
.root {
height: 100%;
width:100%;
grid-template-columns: [navbar] auto [content] 1fr;
display: grid;
}
.navbar {
grid-column: navbar;
background: var(--navBarBackground);
border: var(--navBarBorder);
color: var(--navBarColor);
}
.navitem {
padding: 10px 17px;
cursor: pointer;
}
.navitem:hover {
background: var(--itemHoverBackground);
color: var(--itemHoverColor);
}
.navitem.selected {
background: var(--selectedItemBackground);
border: var(--selectedItemBorder);
color: var(--selectedItemColor);
}
.content {
grid-column: content;
}
</style>