Add global app theme picker and use it in client preview

This commit is contained in:
Andrew Kingston 2021-06-28 12:55:11 +01:00
parent 6f0bbce083
commit f2be9501d3
11 changed files with 93 additions and 1415 deletions

View File

@ -8,7 +8,6 @@ import {
selectedComponent, selectedComponent,
selectedAccessRole, selectedAccessRole,
} from "builderStore" } from "builderStore"
// Backendstores
import { import {
datasources, datasources,
integrations, integrations,
@ -43,6 +42,7 @@ const INITIAL_FRONTEND_STATE = {
appId: "", appId: "",
routes: {}, routes: {},
clientLibPath: "", clientLibPath: "",
theme: "",
} }
export const getFrontendStore = () => { export const getFrontendStore = () => {
@ -62,6 +62,7 @@ export const getFrontendStore = () => {
url: application.url, url: application.url,
layouts, layouts,
screens, screens,
theme: application.theme,
hasAppPackage: true, hasAppPackage: true,
appInstance: application.instance, appInstance: application.instance,
clientLibPath, clientLibPath,
@ -79,6 +80,20 @@ export const getFrontendStore = () => {
database.set(application.instance) database.set(application.instance)
tables.init() tables.init()
}, },
theme: {
save: async theme => {
const appId = get(store).appId
const response = await api.put(`/api/applications/${appId}`, { theme })
if (response.status === 200) {
store.update(state => {
state.theme = theme
return state
})
} else {
throw new Error("Error updating theme")
}
},
},
routing: { routing: {
fetch: async () => { fetch: async () => {
const response = await api.get("/api/routing") const response = await api.get("/api/routing")

View File

@ -0,0 +1,38 @@
<script>
import { Select } from "@budibase/bbui"
import { store } from "builderStore"
const themeOptions = [
{
label: "Lightest",
value: "spectrum--lightest",
},
{
label: "Light",
value: "spectrum--light",
},
{
label: "Dark",
value: "spectrum--dark",
},
{
label: "Darkest",
value: "spectrum--darkest",
},
]
</script>
<div>
<Select
value={$store.theme || "spectrum--lightest"}
options={themeOptions}
placeholder={null}
on:change={e => store.actions.theme.save(e.detail)}
/>
</div>
<style>
div {
padding-right: 8px;
}
</style>

View File

@ -44,6 +44,7 @@
screen, screen,
selectedComponentId, selectedComponentId,
previewType: $store.currentFrontEndType, previewType: $store.currentFrontEndType,
theme: $store.theme,
} }
// Saving pages and screens to the DB causes them to have _revs. // Saving pages and screens to the DB causes them to have _revs.

View File

@ -46,7 +46,14 @@ export default `
} }
// Extract data from message // Extract data from message
const { selectedComponentId, layout, screen, previewType, appId } = JSON.parse(event.data) const {
selectedComponentId,
layout,
screen,
previewType,
appId,
theme
} = JSON.parse(event.data)
// Set some flags so the app knows we're in the builder // Set some flags so the app knows we're in the builder
window["##BUDIBASE_IN_BUILDER##"] = true window["##BUDIBASE_IN_BUILDER##"] = true
@ -56,6 +63,7 @@ export default `
window["##BUDIBASE_SELECTED_COMPONENT_ID##"] = selectedComponentId window["##BUDIBASE_SELECTED_COMPONENT_ID##"] = selectedComponentId
window["##BUDIBASE_PREVIEW_ID##"] = Math.random() window["##BUDIBASE_PREVIEW_ID##"] = Math.random()
window["##BUDIBASE_PREVIEW_TYPE##"] = previewType window["##BUDIBASE_PREVIEW_TYPE##"] = previewType
window["##BUDIBASE_PREVIEW_THEME##"] = theme
// Initialise app // Initialise app
if (window.loadBudibase) { if (window.loadBudibase) {

View File

@ -13,6 +13,7 @@
import { FrontendTypes } from "constants" import { FrontendTypes } from "constants"
import { findComponent, findComponentPath } from "builderStore/storeUtils" import { findComponent, findComponentPath } from "builderStore/storeUtils"
import { get } from "svelte/store" import { get } from "svelte/store"
import AppThemeSelect from "components/design/AppPreview/AppThemeSelect.svelte"
// Cache previous values so we don't update the URL more than necessary // Cache previous values so we don't update the URL more than necessary
let previousType let previousType
@ -147,7 +148,10 @@
<div class="preview-pane"> <div class="preview-pane">
{#if $currentAsset} {#if $currentAsset}
<ComponentSelectionList /> <div class="preview-header">
<ComponentSelectionList />
<AppThemeSelect />
</div>
<div class="preview-content"> <div class="preview-content">
<CurrentItemPreview /> <CurrentItemPreview />
</div> </div>
@ -193,6 +197,10 @@
gap: var(--spacing-m); gap: var(--spacing-m);
padding: var(--spacing-xl) 40px; padding: var(--spacing-xl) 40px;
} }
.preview-header {
display: grid;
grid-template-columns: 1fr 100px;
}
.preview-content { .preview-content {
flex: 1 1 auto; flex: 1 1 auto;
} }

View File

@ -55,6 +55,8 @@
} }
} }
} }
$: themeClass = $builderStore.theme || "spectrum--lightest"
</script> </script>
{#if dataLoaded && $screenStore.activeLayout} {#if dataLoaded && $screenStore.activeLayout}
@ -62,7 +64,7 @@
id="spectrum-root" id="spectrum-root"
lang="en" lang="en"
dir="ltr" dir="ltr"
class="spectrum spectrum--medium spectrum--light" class="spectrum spectrum--medium {themeClass}"
> >
<Provider key="user" data={$authStore} {actions}> <Provider key="user" data={$authStore} {actions}>
<div id="app-root"> <div id="app-root">

View File

@ -13,6 +13,7 @@ const loadBudibase = () => {
selectedComponentId: window["##BUDIBASE_SELECTED_COMPONENT_ID##"], selectedComponentId: window["##BUDIBASE_SELECTED_COMPONENT_ID##"],
previewId: window["##BUDIBASE_PREVIEW_ID##"], previewId: window["##BUDIBASE_PREVIEW_ID##"],
previewType: window["##BUDIBASE_PREVIEW_TYPE##"], previewType: window["##BUDIBASE_PREVIEW_TYPE##"],
theme: window["##BUDIBASE_PREVIEW_THEME##"],
}) })
// Create app if one hasn't been created yet // Create app if one hasn't been created yet

File diff suppressed because it is too large Load Diff

View File

@ -1651,46 +1651,6 @@
"label": "Schema", "label": "Schema",
"key": "dataSource" "key": "dataSource"
}, },
{
"type": "select",
"label": "Theme",
"key": "theme",
"defaultValue": "spectrum--light",
"options": [
{
"label": "Lightest",
"value": "spectrum--lightest"
},
{
"label": "Light",
"value": "spectrum--light"
},
{
"label": "Dark",
"value": "spectrum--dark"
},
{
"label": "Darkest",
"value": "spectrum--darkest"
}
]
},
{
"type": "select",
"label": "Size",
"key": "size",
"defaultValue": "spectrum--medium",
"options": [
{
"label": "Medium",
"value": "spectrum--medium"
},
{
"label": "Large",
"value": "spectrum--large"
}
]
},
{ {
"type": "boolean", "type": "boolean",
"label": "Disabled", "label": "Disabled",
@ -2067,46 +2027,6 @@
"key": "rowCount", "key": "rowCount",
"defaultValue": 8 "defaultValue": 8
}, },
{
"type": "select",
"label": "Theme",
"key": "theme",
"defaultValue": "spectrum--light",
"options": [
{
"label": "Lightest",
"value": "spectrum--lightest"
},
{
"label": "Light",
"value": "spectrum--light"
},
{
"label": "Dark",
"value": "spectrum--dark"
},
{
"label": "Darkest",
"value": "spectrum--darkest"
}
]
},
{
"type": "select",
"label": "Size",
"key": "size",
"defaultValue": "spectrum--medium",
"options": [
{
"label": "Medium",
"value": "spectrum--medium"
},
{
"label": "Large",
"value": "spectrum--large"
}
]
},
{ {
"type": "multifield", "type": "multifield",
"label": "Columns", "label": "Columns",

View File

@ -5,8 +5,6 @@
import { generateID } from "../helpers" import { generateID } from "../helpers"
export let dataSource export let dataSource
export let theme
export let size
export let disabled = false export let disabled = false
export let initialValues export let initialValues
@ -160,14 +158,7 @@
{actions} {actions}
data={{ ...$formState.values, tableId: dataSource?.tableId }} data={{ ...$formState.values, tableId: dataSource?.tableId }}
> >
<div <div use:styleable={$component.styles}>
lang="en"
dir="ltr"
use:styleable={$component.styles}
class={`spectrum ${size || "spectrum--medium"} ${
theme || "spectrum--light"
}`}
>
{#if loaded} {#if loaded}
<slot /> <slot />
{/if} {/if}

View File

@ -3,8 +3,6 @@
import { Table } from "@budibase/bbui" import { Table } from "@budibase/bbui"
import SlotRenderer from "./SlotRenderer.svelte" import SlotRenderer from "./SlotRenderer.svelte"
export let theme
export let size
export let dataProvider export let dataProvider
export let columns export let columns
export let showAutoColumns export let showAutoColumns
@ -73,12 +71,7 @@
} }
</script> </script>
<div <div use:styleable={$component.styles}>
lang="en"
dir="ltr"
use:styleable={$component.styles}
class={`spectrum ${size || "spectrum--medium"} ${theme || "spectrum--light"}`}
>
<Table <Table
{data} {data}
{schema} {schema}