LyteNyte Grid logo for light mode. Links back to the documentation home page.
Theming

Grid Theming With Emotion

LyteNyte Grid can be styled using Emotion CSS (or any other CSS-in-JS library). This guide explains an approach compatible with styled-component style APIs.

Modern React recommends avoiding runtime CSS-in-JS libraries. We include this guide because many codebases still rely on them. LyteNyte Grid does not assume a specific CSS framework, so a styled-component approach works as well as a build-time CSS approach.

Emotion Setup

To start using Emotion, install the @emotion/styled package:

npm install @emotion/styled

This package provides Emotion's styled-component API and lets you create pre-styled components:

import styled from "@emotion/styled";
const Button = styled.button`
color: turquoise;
`;

We apply this pattern to style LyteNyte Grid components.

Creating Styled LyteNyte Grid Components

LyteNyte Grid is headless and exposes root primitives you can style individually. Emotion accepts arbitrary components, and the grid primitives align with Emotion's API, so creating styled components is straightforward. For example, you can create a styled Grid.Cell:

const Cell = styled(Grid.Cell)`
display: flex;
align-items: center;
padding-inline: 8px;
background-color: light-dark(white, hsla(190, 32%, 6%, 1));
color: light-dark(hsla(175, 6%, 38%, 1), hsla(175, 10%, 86%, 1));
font-size: 14px;
border-bottom: 1px solid light-dark(hsla(175, 20%, 95%, 1), hsla(177, 19%, 17%, 1));
`;

Use the Cell component in place of Grid.Cell when building the grid view. The following example demonstrates this and also defines a styled header.

Styling With Emotion

Styling Elements Not Directly Exposed

The Grid Theming guide explains that some elements are not directly exposed through LyteNyte Grid's public component interface. You must style these elements using data attributes or any other selector strategy. When using Emotion, you can target these elements with nested CSS selectors inside a styled wrapper.

Styling Row Detail

To style the row-detail element, wrap the Grid.Viewport or Grid.RowsContainer components in a styled wrapper and target the element with the data-ln-row-detail attribute. The example below shows this approach.

Emotion Styling Row Detail

Here is the styled wrapper used to target the row-detail container:

const RowsContainer = styled(Grid.RowsContainer)`
& [data-ln-row-detail="true"] {
padding: 24px;
}
& [data-ln-row-detail="true"] > div {
border: 1px solid gray;
border-radius: 8px;
background-color: light-dark(white, transparent);
}
`;

Styling Cell Selection Ranges

Cell-selection rectangles can also be styled using a nested data-attribute selector within a styled wrapper. The example below demonstrates this:

Cell Selection Styling With Emotion

Here is the styled wrapper that targets the cell-selection rectangle:

const RowsContainer = styled(Grid.RowsContainer)`
& [data-ln-cell-selection-rect]:not([data-ln-cell-selection-is-unit="true"]) {
background-color: var(--lng1771-primary-30);
box-sizing: border-box;
&[data-ln-cell-selection-border-top="true"] {
border-top: 1px solid var(--lng1771-primary-50);
}
&[data-ln-cell-selection-border-bottom="true"] {
border-bottom: 1px solid var(--lng1771-primary-50);
}
&[data-ln-cell-selection-border-start="true"] {
border-inline-start: 1px solid var(--lng1771-primary-50);
}
&[data-ln-cell-selection-border-end="true"] {
border-inline-end: 1px solid var(--lng1771-primary-50);
}
}
& [data-ln-cell-selection-rect][data-ln-cell-selection-is-unit="true"] {
outline: 1px solid var(--lng1771-primary-50);
outline-offset: -1px;
}
`;

Emotion And Pre-built Themes

LyteNyte Grid provides pre-styled themes implemented in plain CSS. These themes work with any CSS framework, including Emotion. You can include a pre-built theme in your application and layer Emotion-based styles on top. The example below demonstrates this:

Pre-built Themes With Emotion

Next Steps

  • Grid Theming: review the general theming guide to learn more about styling LyteNyte Grid.
  • Columns: learn how column configuration influences the grid view.
  • Cell Renderers: define custom cell renderers to further control cell content.