Skip to content

Commit 78de154

Browse files
committed
fix: use an adopted stylesheet to render the layout
1 parent 900baf8 commit 78de154

5 files changed

+30
-34
lines changed

cosmoz-omnitable.js

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const Omnitable = (host) => {
3636
<style>
3737
${guard([], () => shimCSS(styles))}
3838
</style>
39-
<div id="layoutStyle"></div>
4039
4140
<div class="mainContainer">
4241
${renderHeader(header)}

lib/compute-layout.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { html } from '@pionjs/pion';
21
import { layout } from './layout';
32

4-
const
5-
_toCss = (layout, config) =>
6-
config
7-
.map((item, index) => {
8-
const width = layout[index];
9-
return width == null || width === 0
10-
? `cosmoz-omnitable-resize-nub[name="${ item.name }"], .cell[name="${ item.name }"]{display:none}`
11-
: `.cell[name="${ item.name }"]{width: ${ width }px;padding: 0 min(3px, ${ width / 2 }px)}`;
12-
})
13-
.join('\n');
3+
const _toCss = (layout, config) =>
4+
config
5+
.map((item, index) => {
6+
const width = layout[index];
7+
return width == null || width === 0
8+
? `cosmoz-omnitable-resize-nub[name="${item.name}"], .cell[name="${item.name}"]{display:none}`
9+
: `.cell[name="${item.name}"]{width: ${Math.floor(
10+
width,
11+
)}px;padding: 0 min(3px, ${width / 2}px)}`;
12+
})
13+
.join('\n');
1414

15-
export const
16-
computeLayout = (_columnConfigs, canvasWidth, numColumns) => {
17-
const columnConfigs = _columnConfigs.filter(c => !c.hidden),
15+
export const computeLayout = (_columnConfigs, canvasWidth, numColumns) => {
16+
const columnConfigs = _columnConfigs.filter((c) => !c.hidden),
1817
totalWidths = columnConfigs.reduce((sum, { width }) => sum + width, 0);
1918

2019
if (columnConfigs.length > 1 && totalWidths > canvasWidth) {
@@ -26,8 +25,11 @@ export const
2625
// as the column configs might now be in a different order than displayed
2726
// due to priority settings
2827
const lastColumnIndex = columnConfigs.reduce(
29-
([max, maxIndex], column, index) => [Math.max(max, column.index), column.index > max ? index : maxIndex],
30-
[-1, -1]
28+
([max, maxIndex], column, index) => [
29+
Math.max(max, column.index),
30+
column.index > max ? index : maxIndex,
31+
],
32+
[-1, -1],
3133
)[1];
3234

3335
// force the last visible column to flex
@@ -43,6 +45,4 @@ export const
4345
}, new Array(numColumns).fill(undefined));
4446
},
4547
toCss = (layout, config) =>
46-
layout.length === 0
47-
? html`<style>.cell {display: none;}</style>`
48-
: html`<style>${ _toCss(layout, config) }</style>`;
48+
layout.length === 0 ? '.cell {display: none;}' : _toCss(layout, config);

lib/use-fast-layout.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { useLayoutEffect, useMemo } from '@pionjs/pion';
1+
import { useMemo } from '@pionjs/pion';
22
import { toCss } from './compute-layout';
33
import { useResizableColumns } from './use-resizable-columns';
44
import { useCanvasWidth } from './use-canvas-width';
55
import { useTweenArray } from './use-tween-array';
66
import { useLayout } from './use-layout';
7-
import { render } from 'lit-html';
7+
import { useStyleSheet } from '@neovici/cosmoz-utils/hooks/use-stylesheet';
88

99
export const useFastLayout = ({
1010
host,
@@ -24,7 +24,7 @@ export const useFastLayout = ({
2424
tweenedlayout = useTweenArray(layout, resizeSpeedFactor),
2525
layoutCss = useMemo(
2626
() => toCss(tweenedlayout, settings.columns),
27-
[tweenedlayout]
27+
[tweenedlayout],
2828
),
2929
collapsedColumns = useMemo(
3030
() =>
@@ -35,9 +35,9 @@ export const useFastLayout = ({
3535
column.disabled
3636
? acc
3737
: [...acc, columns.find((c) => c.name === column.name)],
38-
[]
38+
[],
3939
),
40-
[columns, settings, layout]
40+
[columns, settings, layout],
4141
);
4242

4343
useResizableColumns({
@@ -47,10 +47,7 @@ export const useFastLayout = ({
4747
setSettings: (update) => setSettings(update(settings)),
4848
});
4949

50-
useLayoutEffect(
51-
() => render(layoutCss, host.shadowRoot.querySelector('#layoutStyle')),
52-
[layoutCss]
53-
);
50+
useStyleSheet(layoutCss);
5451

5552
return { collapsedColumns };
5653
};

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"@neovici/cosmoz-i18next": "^3.1.1",
6969
"@neovici/cosmoz-input": "^4.0.0",
7070
"@neovici/cosmoz-router": "^11.0.0",
71-
"@neovici/cosmoz-utils": "^6.0.0",
71+
"@neovici/cosmoz-utils": "^6.5.0",
7272
"@neovici/nullxlsx": "^3.0.0",
7373
"@pionjs/pion": "^2.0.0",
7474
"@polymer/iron-icon": "^3.0.0",

0 commit comments

Comments
 (0)