Skip to content

[feat] Update useLayoutStyles to support REM type#12733

Merged
sfc-gh-lwilby merged 1 commit intodevelopfrom
10-09-st-space-update-uselayoutstyles-support-rem
Oct 10, 2025
Merged

[feat] Update useLayoutStyles to support REM type#12733
sfc-gh-lwilby merged 1 commit intodevelopfrom
10-09-st-space-update-uselayoutstyles-support-rem

Conversation

@sfc-gh-lwilby
Copy link
Copy Markdown
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby commented Oct 9, 2025

Describe your changes

Adds support to useLayoutStyles for REM type in preparation for st.space which will support REM options.

GitHub Issue Link (if applicable)

Testing Plan

  • Unit Tests (JS and/or Python) ✅

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Oct 9, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 9, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-12733/streamlit-1.50.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-12733.streamlit.app (☁️ Deploy here if not accessible)

Copy link
Copy Markdown
Collaborator Author

sfc-gh-lwilby commented Oct 9, 2025

@sfc-gh-lwilby sfc-gh-lwilby added security-assessment-completed impact:internal PR changes only affect internal code change:feature PR contains new feature or enhancement implementation labels Oct 9, 2025
@sfc-gh-lwilby sfc-gh-lwilby changed the title st-space-update-useLayoutStyles-support-rem [feat] Update useLayoutStyles to support REM type Oct 9, 2025
@sfc-gh-lwilby sfc-gh-lwilby marked this pull request as ready for review October 9, 2025 13:02
@lukasmasuch lukasmasuch requested a review from Copilot October 9, 2025 18:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for REM (relative unit) type to the useLayoutStyles hook in preparation for an upcoming st.space feature that will support REM options. The change extends the existing layout system to handle REM units alongside the current pixel, stretch, and content dimension types.

Key changes:

  • Added REM as a new dimension type with corresponding logic for width and height calculations
  • Extended type definitions and function signatures to support REM values
  • Added comprehensive test coverage for REM dimension handling

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
frontend/lib/src/components/core/Layout/useLayoutStyles.ts Adds REM dimension type support, extends width/height calculation logic, and updates function signatures
frontend/lib/src/components/core/Layout/useLayoutStyles.test.tsx Adds comprehensive test cases for REM width, height, and flex behavior

Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Comment on lines 59 to 63
type LayoutDimensionConfig = {
type: DimensionType | undefined
pixels?: number | undefined
rem?: number | undefined
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider using a data structure that enforces required fields on a per-type basis.

Right now, this is a valid object per this type that will pass the type-checker:

const dimension: LayoutDimensionConfig = { type: "pixel", rem: 100 }

This is not actually correct, and it would be good for the compiler to prevent these kinds of situations to begin with.

We can use discriminated unions to achieve type safety in this scenario. For example:

type LayoutDimensionConfig =
  | { type: "stretch" }
  | { type: "content" }
  | { type: "pixel"; pixels: number }
  | { type: "rem"; rem: number }

Now, the same example from before becomes a compiler error!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, thanks!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfc-gh-bnisco had to make a lot of changes in useLayoutStyles to support this. Could you take another look since it is quite different since Lukas approved now.

Base automatically changed from 10-07-st-space-proto-files to develop October 10, 2025 11:39
@sfc-gh-lwilby sfc-gh-lwilby requested a review from a team as a code owner October 10, 2025 11:39
@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 10-09-st-space-update-uselayoutstyles-support-rem branch 2 times, most recently from 7cdc6e1 to 928bdf6 Compare October 10, 2025 17:11
Copy link
Copy Markdown
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

let width: React.CSSProperties["width"] = "auto"
if (widthType === DimensionType.STRETCH) {
if (widthConfig.type === DimensionType.STRETCH) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Now that we have a union type to work with, another common pattern is to utilize a switch with assertNever on these code paths in order to get exhaustive compiler-time type checking.

Something like:

const { type } = widthConfig

switch (type) {
  case DimensionType.STRETCH:
    width = "100%"
  case DimensionType.REM:
    width = `${widthConfig.rem}rem`
  // ... all the other cases
  default:
    assertNever(type)
}

The benefit of this approach is that if the type union were to add a new member, this switch statement would become a compiler error until the new case is added. The intention here is to have the compiler proactively alert to the areas that need to be updated.

@sfc-gh-lwilby sfc-gh-lwilby force-pushed the 10-09-st-space-update-uselayoutstyles-support-rem branch from 928bdf6 to ee4f058 Compare October 10, 2025 17:51
@sfc-gh-lwilby sfc-gh-lwilby merged commit 3ac6354 into develop Oct 10, 2025
39 checks passed
@sfc-gh-lwilby sfc-gh-lwilby deleted the 10-09-st-space-update-uselayoutstyles-support-rem branch October 10, 2025 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:internal PR changes only affect internal code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants