An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
feat: configure initial UI state with defaultUIConfig (#527)
Elwynn Chen9c8fa2d8effc
diff --git a/README.md b/README.mdindex 84028d6e..91f15c7e 100644--- a/README.md+++ b/README.md@@ -124,6 +124,24 @@ const YourEmbeddingApp: React.FC<IYourEmbeddingAppProps> = props => {export default YourEmbeddingApp;```+Use `defaultUIConfig` to set the editor's initial UI state. For example, start with the Auto Viz panel collapsed:++```tsx+<GraphicWalker+ data={data}+ fields={fields}+ defaultUIConfig={{+ panels: {+ autoViz: { expanded: false },+ },+ }}+/>+```++The collapsed panel can still be opened with its expand button or the Auto Viz toolbar toggle. Omitting `panels.autoViz.expanded` keeps the panel expanded by default.++UI defaults apply only when the visualization store is created. Updating `defaultUIConfig`, supplying a new `spec` or `chart`, or switching charts does not reset the user's panel state. A reused `keepAlive` store also retains its state. Use `defaultConfig` for chart configuration and layout defaults, and `defaultTab` for the initial tab.+If you have a configuration of GraphicWalker chart, you can use the `PureRenderer` or `GraphicRenderer` component to make a single chart without controls UI.```typescriptdiff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsxindex b4ea20bc..6c433d45 100644--- a/packages/graphic-walker/src/App.tsx+++ b/packages/graphic-walker/src/App.tsx@@ -308,7 +308,7 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {});export function VizAppWithContext(props: IVizAppProps & IComputationProps) {- const { computation, onMetaChange, fieldKeyGuard, keepAlive, storeRef, defaultConfig, defaultTab, defaultRenderer, ...rest } = props;+ const { computation, onMetaChange, fieldKeyGuard, keepAlive, storeRef, defaultConfig, defaultUIConfig, defaultTab, defaultRenderer, ...rest } = props;// @TODO remove deprecated propsconst appearance = props.appearance ?? props.dark;const data = props.data ?? props.dataSource;@@ -354,6 +354,7 @@ export function VizAppWithContext(props: IVizAppProps & IComputationProps) {keepAlive={keepAlive}storeRef={storeRef}defaultConfig={defaultConfig}+ defaultUIConfig={defaultUIConfig}defaultTab={defaultTab}defaultRenderer={defaultRenderer}>diff --git a/packages/graphic-walker/src/Renderer.tsx b/packages/graphic-walker/src/Renderer.tsxindex 1697dbda..31b74fcc 100644--- a/packages/graphic-walker/src/Renderer.tsx+++ b/packages/graphic-walker/src/Renderer.tsx@@ -278,7 +278,7 @@ const FilterSection = observer(function FilterSection() {export function RendererAppWithContext(props: IVizAppProps & IComputationProps & { overrideSize?: IVisualLayout['size']; containerClassName?: string; containerStyle?: React.CSSProperties }) {- const { dark, dataSource, computation, onMetaChange, fieldKeyGuard, keepAlive, storeRef, defaultConfig, defaultRenderer, ...rest } = props;+ const { dark, dataSource, computation, onMetaChange, fieldKeyGuard, keepAlive, storeRef, defaultConfig, defaultUIConfig, defaultRenderer, ...rest } = props;// @TODO remove deprecated propsconst appearance = props.appearance ?? props.dark;const data = props.data ?? props.dataSource;@@ -319,7 +319,15 @@ export function RendererAppWithContext(const darkMode = useCurrentMediaTheme(appearance);return (- <VizStoreWrapper onMetaChange={safeOnMetaChange} meta={safeMetas} keepAlive={keepAlive} storeRef={storeRef} defaultConfig={defaultConfig} defaultRenderer={defaultRenderer}>+ <VizStoreWrapper+ onMetaChange={safeOnMetaChange}+ meta={safeMetas}+ keepAlive={keepAlive}+ storeRef={storeRef}+ defaultConfig={defaultConfig}+ defaultUIConfig={defaultUIConfig}+ defaultRenderer={defaultRenderer}+ ><RendererApp {...rest} darkMode={darkMode} computation={safeComputation} /></VizStoreWrapper>);diff --git a/packages/graphic-walker/src/Table.tsx b/packages/graphic-walker/src/Table.tsxindex e59952aa..614dee13 100644--- a/packages/graphic-walker/src/Table.tsx+++ b/packages/graphic-walker/src/Table.tsx@@ -114,7 +114,7 @@ export const TableApp = observer(function VizApp(props: BaseTableProps) {});export function TableAppWithContext(props: ITableProps & IComputationProps) {- const { dark, dataSource, computation, onMetaChange, fieldKeyGuard, keepAlive, storeRef, defaultConfig, defaultRenderer, ...rest } = props;+ const { dark, dataSource, computation, onMetaChange, fieldKeyGuard, keepAlive, storeRef, defaultConfig, defaultUIConfig, defaultRenderer, ...rest } = props;// @TODO remove deprecated propsconst appearance = props.appearance ?? props.dark;const data = props.data ?? props.dataSource;@@ -157,7 +157,15 @@ export function TableAppWithContext(props: ITableProps & IComputationProps) {const darkMode = useCurrentMediaTheme(appearance);return (- <VizStoreWrapper onMetaChange={safeOnMetaChange} meta={safeMetas} keepAlive={keepAlive} storeRef={storeRef} defaultConfig={defaultConfig} defaultRenderer={defaultRenderer}>+ <VizStoreWrapper+ onMetaChange={safeOnMetaChange}+ meta={safeMetas}+ keepAlive={keepAlive}+ storeRef={storeRef}+ defaultConfig={defaultConfig}+ defaultUIConfig={defaultUIConfig}+ defaultRenderer={defaultRenderer}+ ><TableApp darkMode={darkMode} computation={safeComputation} {...rest} /></VizStoreWrapper>);diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.tsindex fe0e65b7..bcd98b53 100644--- a/packages/graphic-walker/src/interfaces.ts+++ b/packages/graphic-walker/src/interfaces.ts@@ -1018,6 +1018,16 @@ export interface IDefaultConfig {layout?: Partial<IVisualLayout>;}+/** Initial editor UI state, applied only when a visualization store is created. */+export interface IDefaultUIConfig {+ panels?: {+ autoViz?: {+ /** Whether the Auto Viz panel starts expanded. The feature remains available when collapsed. @default true */+ expanded?: boolean;+ };+ };+}+export interface IVizStoreProps {storeRef?: React.RefObject<VizSpecStore | null>;keepAlive?: boolean | string;@@ -1026,6 +1036,8 @@ export interface IVizStoreProps {fields?: IMutField[];onMetaChange?: (fid: string, meta: Partial<IMutField>) => void;defaultConfig?: IDefaultConfig;+ /** Initial UI defaults. Changes do not override user actions or a reused keepAlive store. */+ defaultUIConfig?: IDefaultUIConfig;defaultRenderer?: 'vega-lite' | 'observable-plot';}diff --git a/packages/graphic-walker/src/store/index.test.tsx b/packages/graphic-walker/src/store/index.test.tsxnew file mode 100644index 00000000..b6cdbd39--- /dev/null+++ b/packages/graphic-walker/src/store/index.test.tsx@@ -0,0 +1,61 @@+import React from 'react';+import { renderToStaticMarkup } from 'react-dom/server';+import { VizStoreWrapper, useVizStore } from './index';+import type { VizSpecStore } from './visualSpecStore';++function renderStore(props: Omit<React.ComponentProps<typeof VizStoreWrapper>, 'children'>) {+ let store!: VizSpecStore;+ function PanelState() {+ store = useVizStore();+ return <span>{store.showAutoVizPanel ? 'expanded' : 'collapsed'}</span>;+ }++ const markup = renderToStaticMarkup(+ <VizStoreWrapper {...props}>+ <PanelState />+ </VizStoreWrapper>+ );+ return { store, markup };+}++describe('VizStoreWrapper UI defaults', () => {+ test('provides a collapsed panel on the first render without waiting for effects', () => {+ const { markup } = renderStore({ meta: [], defaultUIConfig: { panels: { autoViz: { expanded: false } } } });++ expect(markup).toBe('<span>collapsed</span>');+ });++ test('keeps the panel expanded when UI defaults are omitted', () => {+ const { markup } = renderStore({ meta: [] });++ expect(markup).toBe('<span>expanded</span>');+ });++ test('preserves user state on keepAlive remounts even when default props change', () => {+ const keepAlive = 'default-ui-config-remount';+ const initial = renderStore({ meta: [], keepAlive, defaultUIConfig: { panels: { autoViz: { expanded: false } } } });+ initial.store.setShowAutoVizPanel(true);++ const reopened = renderStore({ meta: [], keepAlive, defaultUIConfig: { panels: { autoViz: { expanded: false } } } });+ expect(reopened.store).toBe(initial.store);+ expect(reopened.markup).toBe('<span>expanded</span>');++ reopened.store.setShowAutoVizPanel(false);+ const changedDefaults = renderStore({ meta: [], keepAlive, defaultUIConfig: { panels: { autoViz: { expanded: true } } } });++ expect(changedDefaults.store).toBe(initial.store);+ expect(changedDefaults.markup).toBe('<span>collapsed</span>');+ });++ test('applies the supplied defaults when the keepAlive key creates a new store', () => {+ const initial = renderStore({ meta: [], keepAlive: 'default-ui-config-before-key-change' });+ const next = renderStore({+ meta: [],+ keepAlive: 'default-ui-config-after-key-change',+ defaultUIConfig: { panels: { autoViz: { expanded: false } } },+ });++ expect(next.store).not.toBe(initial.store);+ expect(next.markup).toBe('<span>collapsed</span>');+ });+});diff --git a/packages/graphic-walker/src/store/index.tsx b/packages/graphic-walker/src/store/index.tsxindex c9bfc1a6..23fd7c31 100644--- a/packages/graphic-walker/src/store/index.tsx+++ b/packages/graphic-walker/src/store/index.tsx@@ -1,6 +1,6 @@import React, { useContext, useMemo, useEffect, createContext } from 'react';import { VizSpecStore } from './visualSpecStore';-import { IComputationFunction, IDefaultConfig, IMutField, IRow, ISegmentKey } from '../interfaces';+import { IComputationFunction, IDefaultConfig, IDefaultUIConfig, IMutField, IRow, ISegmentKey } from '../interfaces';function createKeepAliveContext<T, U extends any[]>(create: (...args: U) => T) {const dict: Record<string, T> = {};@@ -21,6 +21,7 @@ const getVizStore = createKeepAliveContext(empty?: boolean;onMetaChange?: (fid: string, diffMeta: Partial<IMutField>) => void;defaultConfig?: IDefaultConfig;+ defaultUIConfig?: IDefaultUIConfig;defaultTab?: 'data' | 'visualization';}) => new VizSpecStore(meta, { ...opts, initialSegment: opts?.defaultTab === 'data' ? ISegmentKey.data : ISegmentKey.vis })@@ -37,12 +38,14 @@ interface VizStoreWrapperProps {meta: IMutField[];onMetaChange?: (fid: string, meta: Partial<IMutField>) => void;defaultConfig?: IDefaultConfig;+ defaultUIConfig?: IDefaultUIConfig;defaultTab?: 'data' | 'visualization';defaultRenderer?: 'vega-lite' | 'observable-plot';}export const VizStoreWrapper = (props: VizStoreWrapperProps) => {const storeKey = props.keepAlive ? `${props.keepAlive}` : '';+ // UI defaults only apply to a new store; rerenders and keepAlive reuse preserve user state.const store = useMemo(() => {const defaultConfig = props.defaultRenderer? {@@ -50,8 +53,12 @@ export const VizStoreWrapper = (props: VizStoreWrapperProps) => {layout: { renderer: props.defaultRenderer, ...(props.defaultConfig?.layout ?? {}) },}: props.defaultConfig;- return getVizStore(storeKey, props.meta, { onMetaChange: props.onMetaChange, defaultConfig, defaultTab: props.defaultTab });- // IMPORTANT the store is only associated with the storeKey+ return getVizStore(storeKey, props.meta, {+ onMetaChange: props.onMetaChange,+ defaultConfig,+ defaultUIConfig: props.defaultUIConfig,+ defaultTab: props.defaultTab,+ });}, [storeKey]);// Keep this effect before metadata sync: first hydration reads the currentdiff --git a/packages/graphic-walker/src/store/visualSpecStore.test.ts b/packages/graphic-walker/src/store/visualSpecStore.test.tsindex d2913a7b..b275a944 100644--- a/packages/graphic-walker/src/store/visualSpecStore.test.ts+++ b/packages/graphic-walker/src/store/visualSpecStore.test.ts@@ -1,4 +1,4 @@-import { ISegmentKey, type IMutField } from '../interfaces';+import { ISegmentKey, type IDefaultUIConfig, type IMutField } from '../interfaces';import { newChart } from '../models/visSpecHistory';import { VizSpecStore } from './visualSpecStore';@@ -18,7 +18,7 @@ const metaFieldIds = (store: VizSpecStore) => {.filter((fid) => expectedIds.has(fid));};-describe('VizSpecStore metadata updates', () => {+describe('VizSpecStore', () => {beforeAll(() => {Object.defineProperty(globalThis, 'document', {configurable: true,@@ -175,6 +175,62 @@ describe('VizSpecStore metadata updates', () => {expect(store.currentVis.layout.renderer).toBe('observable-plot');});++ describe('default UI configuration', () => {+ test.each<[string, IDefaultUIConfig | undefined, boolean]>([+ ['omitted config', undefined, true],+ ['empty config', {}, true],+ ['empty panels', { panels: {} }, true],+ ['empty Auto Viz defaults', { panels: { autoViz: {} } }, true],+ ['expanded Auto Viz', { panels: { autoViz: { expanded: true } } }, true],+ ['collapsed Auto Viz', { panels: { autoViz: { expanded: false } } }, false],+ ])('initializes %s', (_name, defaultUIConfig, expanded) => {+ const store = new VizSpecStore(META, { defaultUIConfig });++ expect(store.showAutoVizPanel).toBe(expanded);+ });++ test('preserves user panel state through metadata hydration and chart operations', () => {+ const store = new VizSpecStore([], { defaultUIConfig: { panels: { autoViz: { expanded: false } } } });++ store.setShowAutoVizPanel(true);+ store.setMeta(META);+ expect(store.showAutoVizPanel).toBe(true);++ store.replaceNow(newChart(META, 'Controlled chart'));+ expect(store.showAutoVizPanel).toBe(true);++ store.addVisualization();+ store.selectVisualization(0);+ expect(store.showAutoVizPanel).toBe(true);++ store.setShowAutoVizPanel(false);+ store.resetVisualization();+ expect(store.showAutoVizPanel).toBe(false);+ });++ test('keeps UI defaults and user toggles outside the chart and its undo history', () => {+ const store = new VizSpecStore(META, {+ defaultConfig: { layout: { renderer: 'observable-plot' } },+ defaultUIConfig: { panels: { autoViz: { expanded: false } } },+ });+ const initialChart = store.currentVis;++ expect(initialChart.layout.renderer).toBe('observable-plot');+ expect(store.canUndo).toBe(false);++ store.setShowAutoVizPanel(true);+ expect(store.currentVis).toBe(initialChart);+ expect(store.canUndo).toBe(false);++ store.moveField('dimensions', 0, 'columns', 0);+ store.setShowAutoVizPanel(false);+ store.undo();++ expect(store.showAutoVizPanel).toBe(false);+ expect(store.currentEncodings.columns).toEqual([]);+ });+ });});describe('VizSpecStore initial segment', () => {diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.tsindex a2e00d90..b35356a3 100644--- a/packages/graphic-walker/src/store/visualSpecStore.ts+++ b/packages/graphic-walker/src/store/visualSpecStore.ts@@ -40,6 +40,7 @@ import {IPaintMap,IPaintMapV2,IDefaultConfig,+ IDefaultUIConfig,FieldIdentifier,} from '../interfaces';import { GLOBAL_CONFIG } from '../config';@@ -71,8 +72,8 @@ export class VizSpecStore {segmentKey: ISegmentKey = ISegmentKey.vis;showInsightBoard: boolean = false;showDataBoard: boolean = false;- // the Auto Viz dock on the right of the workspace starts expanded; the toolbar toggle collapses it- showAutoVizPanel: boolean = true;+ // Workspace UI state is initialized once and stays outside the chart history.+ showAutoVizPanel: boolean;vizEmbededMenu: { show: boolean; position: [number, number] } = { show: false, position: [0, 0] };showDataConfig: boolean = false;showCodeExportPanel: boolean = false;@@ -105,11 +106,13 @@ export class VizSpecStore {empty?: boolean;onMetaChange?: (fid: string, diffMeta: Partial<IMutField>) => void;defaultConfig?: IDefaultConfig;+ defaultUIConfig?: IDefaultUIConfig;initialSegment?: ISegmentKey;},) {this.meta = meta;this.segmentKey = options?.initialSegment ?? ISegmentKey.vis;+ this.showAutoVizPanel = options?.defaultUIConfig?.panels?.autoViz?.expanded ?? true;this.visList = options?.empty ? [] : [fromFields(meta, 'Chart 1', options?.defaultConfig)];this.pristineInitialCharts = meta.length === 0 ? new Set(this.visList) : new Set();this.createdVis = this.visList.length;