grid icon indicating copy to clipboard operation
grid copied to clipboard

Data Grid settings to 1x click instead of 2x click (svelte)

Open kwladyka opened this issue 4 months ago • 6 comments

Please add settings to use 1x mouse click instead of 2x mouse click to edit a fields in data grid.

1x click gives way better experience, than 2x click. At least for my use cases.

Image

kwladyka avatar Jul 07 '26 22:07 kwladyka

Hello @kwladyka,

It is currently possible to do so by listening to the "focus-cell" event and invoking the "open-editor" event:

Code Snippet
<script>
  import { Willow, Grid } from "@svar-ui/svelte-grid";
  import { data, columns } from "./data.js" 
  
  function init(api) {
    let editorCell = null;
    let pendingClose = null;
  
    const { editor } = api.getReactiveState();
    editor.subscribe(v => {
      if (v) editorCell = { id: v.id, column: v.column };
    });
  
    api.on("close-editor", () => {
      pendingClose = editorCell;
      setTimeout(() => (pendingClose = null));
    });
  
    api.on("focus-cell", ({ row, column, eventSource }) => {
      if (eventSource !== "click" || !row || !column) return;
  
      if (
        pendingClose &&
        pendingClose.id === row &&
        pendingClose.column === column
      ) {
        pendingClose = null;
        return;
      }
  
      api.exec("open-editor", { id: row, column });
    });
  }
</script>

<Willow>
  <Grid {init} {data} {columns} />
</Willow>

Demo: https://snippet.svar.dev/svelte/S8fgBE6ry

When editor closes, it refocuses the cell, which means it would instantly reopen the editor. To prevent that, we listen to "close-editor" to temporarily save currently selected cell, and compare it to the cell passed to the "open-editor" event. If they are equal, we return early from the event and the editor won't reopen.

There are plans to make this easier in the future. If you have any more questions, please let us know.

segfault44 avatar Jul 10 '26 12:07 segfault44

Thank you. I have a follow up question to determine if it is a bug. Any way to fix deselecting cell / row after click on another table? It works when click not on any table.

  1. click on cell in table 1
  2. click on cell in table 2

in step 2) the selected cell in 1) should disappear, but it doesn't.

It works for:

  1. click on cell in table 1
  2. click outside of the table 1, but not on table 2
Image

https://snippet.svar.dev/svelte/ukCPjBAGE

To summary this up, I want to:

  1. deselect cell after click anywhere outside of the table
  2. deselect row after click anywhere outside of the table

kwladyka avatar Jul 11 '26 17:07 kwladyka

Hello,

I was able to identify the issue. It was introduced in the previous workaround. The fix is to defer the "open-editor" call by wrapping it into a setTimeout:

setTimeout(() => api.exec("open-editor", { id: row, column }));
Updated Code Snippet
<script>
  import { Willow, Grid } from "@svar-ui/svelte-grid";
  import { data, columns } from "./data.js" 
  
  function init(api) {
    let editorCell = null;
    let pendingClose = null;
  
    const { editor } = api.getReactiveState();
    editor.subscribe(v => {
      if (v) editorCell = { id: v.id, column: v.column };
    });
  
    api.on("close-editor", () => {
      pendingClose = editorCell;
      setTimeout(() => (pendingClose = null));
    });
  
    api.on("focus-cell", ({ row, column, eventSource }) => {
      if (eventSource !== "click" || !row || !column) return;
  
      if (
        pendingClose &&
        pendingClose.id === row &&
        pendingClose.column === column
      ) {
        pendingClose = null;
        return;
      }
  
      setTimeout(() => api.exec("open-editor", { id: row, column }));
    });
  }
</script>

<div>
Table 1

<Willow>
  <Grid {init} {data} {columns} />
</Willow>

Table 2

<Willow>
  <Grid {init} {data} {columns} />
</Willow>
</div>

Demo: https://snippet.svar.dev/svelte/5mCW4aM1y

It was caused by the fact that "open-editor" swapped cell's DOM node during the click event's bubble phase, which made the click-outside handler to fail.

Note that row selection (highlighted rows) staying in the grid after clicking away is not a bug. You can also turn selection off entirely by specifying select={false} on a grid, or modify its appearance using CSS variables (--wx-table-select-* variables).

segfault44 avatar Jul 13 '26 11:07 segfault44

@segfault44 Thank you ❤️

It seems to work. It will be great to have it as configuration instead of workaround. To summary this up for settings:

  1. 1 click vs 2 click
  2. keep selection after lost focus or not for row
  3. keep selection after lost focus or not for cell

If there is no plan, then it is worth to consider to add the example into official doc. I mean, I can be wrong, but probably many people have the same concern. Workaround is simple, but not straight forward without deep knowledge about Svar. AI didn't figure out this too :)

kwladyka avatar Jul 13 '26 14:07 kwladyka

@segfault44 Is there any workaround trick for deselect row when click outside the table? I can't figure out this.

I did something like this:

	let interestContainer: HTMLElement | undefined = $state();

	function initInterest(api: any) {
		interestApi = api;
	}

	function clearInterestSelection() {
		const selectedId = interestApi?.getState().selectedRows?.[0];
		if (selectedId !== undefined) {
			interestApi.exec('select-row', { id: selectedId, toggle: true });
		}
	}

	function handleWindowClick(event: MouseEvent) {
		if (interestContainer && !interestContainer.contains(event.target as Node)) {
			clearInterestSelection();
		}
	}

	onMount(() => {
		window.addEventListener('click', handleWindowClick);
		return () => window.removeEventListener('click', handleWindowClick);
	});

it works, but it is too complex.

kwladyka avatar Jul 22 '26 17:07 kwladyka

Hello @kwladyka,

The SVAR Grid library comes with a lib-dom helper library, that contains a clickOutside function, which can be used as a Svelte action (the use: directive). As the name suggests, it fires when the user clicks outside of a container it's used on.

Here's the revised example:

Code
<script>
	import { Willow, Grid } from "@svar-ui/svelte-grid";
	import { clickOutside } from "@svar-ui/lib-dom";
	import { data, columns } from "./data.js";

	function init(api) {
		let editorCell = null;
		let pendingClose = null;

		const { editor } = api.getReactiveState();
		editor.subscribe(v => {
			if (v) editorCell = { id: v.id, column: v.column };
		});

		api.on("close-editor", () => {
			pendingClose = editorCell;
			setTimeout(() => (pendingClose = null));
		});

		api.on("focus-cell", ({ row, column, eventSource }) => {
			if (eventSource !== "click" || !row || !column) return;

			if (
				pendingClose &&
				pendingClose.id === row &&
				pendingClose.column === column
			) {
				pendingClose = null;
				return;
			}

			setTimeout(() => api.exec("open-editor", { id: row, column }));
		});
	}

	function deselect(api) {
		return () => {
			if (!api) return;
			[...api.getState().selectedRows].forEach(id =>
				api.exec("select-row", { id, toggle: true })
			);
		};
	}

	let api1 = $state();
	let api2 = $state();
</script>

<div>
	Table 1

	<Willow>
		<div style:width="max-content" use:clickOutside={deselect(api1)}>
			<Grid bind:this={api1} {init} {data} {columns} />
		</div>
	</Willow>

	Table 2

	<Willow>
		<div style:width="max-content" use:clickOutside={deselect(api2)}>
			<Grid bind:this={api2} {init} {data} {columns} />
		</div>
	</Willow>
</div>

Demo: https://snippet.svar.dev/svelte/0SggiSQ6r

And here is a diff to make the changes easier to spot:

Code Diff
@@ -1,5 +1,6 @@
 <script>
        import { Willow, Grid } from "@svar-ui/svelte-grid";
+       import { clickOutside } from "@svar-ui/lib-dom";
        import { data, columns } from "./data.js";
 
        function init(api) {
@@ -31,18 +32,34 @@
                        setTimeout(() => api.exec("open-editor", { id: row, column }));
                });
        }
+
+       function deselect(api) {
+               return () => {
+                       if (!api) return;
+                       [...api.getState().selectedRows].forEach(id =>
+                               api.exec("select-row", { id, toggle: true })
+                       );
+               };
+       }
+
+       let api1 = $state();
+       let api2 = $state();
 </script>
 
 <div>
        Table 1
 
        <Willow>
-               <Grid {init} {data} {columns} />
+               <div style:width="max-content" use:clickOutside={deselect(api1)}>
+                       <Grid bind:this={api1} {init} {data} {columns} />
+               </div>
        </Willow>
 
        Table 2
 
        <Willow>
-               <Grid {init} {data} {columns} />
+               <div style:width="max-content" use:clickOutside={deselect(api2)}>
+                       <Grid bind:this={api2} {init} {data} {columns} />
+               </div>
        </Willow>
 </div>

You can get the API from a SVAR widget by binding to it (e.g. bind:this={api}). The clickOutside action accepts a callback, so deselect() returns a function, that toggles selection on every selected row in the provided API.

If you need any further assistance, please let us know, we'll be happy to help.

segfault44 avatar Jul 23 '26 09:07 segfault44