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

74 lines
1.6 KiB
Svelte
Raw Normal View History

2021-06-04 08:57:18 +02:00
<script>
import { getContext } from "svelte"
import Placeholder from "./Placeholder.svelte"
2021-06-04 08:57:18 +02:00
const { styleable, builderStore } = getContext("sdk")
2021-06-04 08:57:18 +02:00
const component = getContext("component")
2021-06-04 08:57:18 +02:00
export let type = "mainSidebar"
2021-06-11 14:47:57 +02:00
export let minSize = 250
2021-06-04 14:34:52 +02:00
let layoutMap = {
mainSidebar: 2,
sidebarMain: 2,
twoColumns: 2,
threeColumns: 3,
}
2021-06-04 15:52:05 +02:00
let containerWidth
$: columnsDependingOnSize = calculateColumns(containerWidth)
function calculateColumns(parentWidth) {
2021-06-11 14:47:57 +02:00
const numberOfAllowedColumns = Math.floor(parentWidth / minSize) || 100
2021-06-04 15:52:05 +02:00
if (layoutMap[type] <= numberOfAllowedColumns) {
return false
} else if (layoutMap[type] > numberOfAllowedColumns) {
return numberOfAllowedColumns
}
}
2021-06-04 08:57:18 +02:00
</script>
<div
2021-06-04 15:52:05 +02:00
bind:clientWidth={containerWidth}
class="{type} columns-{columnsDependingOnSize}"
use:styleable={$component.styles}
2021-06-04 08:57:18 +02:00
>
<slot />
2021-06-04 14:34:52 +02:00
{#if layoutMap[type] - $component.children > 0}
{#each new Array(layoutMap[type] - $component.children) as _}
<div class:placeholder={$builderStore.inBuilder}>
<Placeholder />
</div>
2021-06-04 14:34:52 +02:00
{/each}
{/if}
2021-06-04 08:57:18 +02:00
</div>
<style>
div {
display: grid;
grid-gap: 16px;
}
.mainSidebar {
grid-template-columns: 3fr 1fr;
2021-06-04 08:57:18 +02:00
}
.sidebarMain {
grid-template-columns: 1fr 3fr;
2021-06-04 08:57:18 +02:00
}
.twoColumns {
grid-template-columns: 1fr 1fr;
}
.threeColumns {
grid-template-columns: 1fr 1fr 1fr;
}
2021-06-04 15:52:05 +02:00
.columns-1 {
grid-template-columns: 1fr;
}
.columns-2 {
grid-template-columns: 1fr 1fr;
}
.placeholder {
border: 2px dashed var(--spectrum-global-color-gray-600);
padding: var(--spacing-l);
}
2021-06-04 08:57:18 +02:00
</style>