Skip to content

Commit

Permalink
fix: use an adopted stylesheet to render the layout
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinecula committed Feb 29, 2024
1 parent 900baf8 commit 78de154
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
1 change: 0 additions & 1 deletion cosmoz-omnitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const Omnitable = (host) => {
<style>
${guard([], () => shimCSS(styles))}
</style>
<div id="layoutStyle"></div>
<div class="mainContainer">
${renderHeader(header)}
Expand Down
38 changes: 19 additions & 19 deletions lib/compute-layout.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { html } from '@pionjs/pion';
import { layout } from './layout';

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

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

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

// force the last visible column to flex
Expand All @@ -43,6 +45,4 @@ export const
}, new Array(numColumns).fill(undefined));
},
toCss = (layout, config) =>
layout.length === 0
? html`<style>.cell {display: none;}</style>`
: html`<style>${ _toCss(layout, config) }</style>`;
layout.length === 0 ? '.cell {display: none;}' : _toCss(layout, config);
15 changes: 6 additions & 9 deletions lib/use-fast-layout.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useLayoutEffect, useMemo } from '@pionjs/pion';
import { useMemo } from '@pionjs/pion';
import { toCss } from './compute-layout';
import { useResizableColumns } from './use-resizable-columns';
import { useCanvasWidth } from './use-canvas-width';
import { useTweenArray } from './use-tween-array';
import { useLayout } from './use-layout';
import { render } from 'lit-html';
import { useStyleSheet } from '@neovici/cosmoz-utils/hooks/use-stylesheet';

export const useFastLayout = ({
host,
Expand All @@ -24,7 +24,7 @@ export const useFastLayout = ({
tweenedlayout = useTweenArray(layout, resizeSpeedFactor),
layoutCss = useMemo(
() => toCss(tweenedlayout, settings.columns),
[tweenedlayout]
[tweenedlayout],
),
collapsedColumns = useMemo(
() =>
Expand All @@ -35,9 +35,9 @@ export const useFastLayout = ({
column.disabled
? acc
: [...acc, columns.find((c) => c.name === column.name)],
[]
[],
),
[columns, settings, layout]
[columns, settings, layout],
);

useResizableColumns({
Expand All @@ -47,10 +47,7 @@ export const useFastLayout = ({
setSettings: (update) => setSettings(update(settings)),
});

useLayoutEffect(
() => render(layoutCss, host.shadowRoot.querySelector('#layoutStyle')),
[layoutCss]
);
useStyleSheet(layoutCss);

return { collapsedColumns };
};
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@neovici/cosmoz-i18next": "^3.1.1",
"@neovici/cosmoz-input": "^4.0.0",
"@neovici/cosmoz-router": "^11.0.0",
"@neovici/cosmoz-utils": "^6.0.0",
"@neovici/cosmoz-utils": "^6.5.0",
"@neovici/nullxlsx": "^3.0.0",
"@pionjs/pion": "^2.0.0",
"@polymer/iron-icon": "^3.0.0",
Expand Down

0 comments on commit 78de154

Please sign in to comment.