Skip to content

Commit

Permalink
Move small and bold styles from global styles to markdown component (s…
Browse files Browse the repository at this point in the history
…treamlit#9821)

## Describe your changes

This PR removes all the `small/strong/b`-related styles from global
styles and applies them in the Markdown component instead.

## Testing Plan

- No logical or visual changes. Existing tests should be sufficient.

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
  • Loading branch information
lukasmasuch authored Nov 13, 2024
1 parent e91db4b commit dfa5f37
Show file tree
Hide file tree
Showing 39 changed files with 46 additions and 62 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions e2e_playwright/app_hotkeys_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
def test_shows_clear_cache_dialog_when_c_is_pressed(app: Page):
app.keyboard.type("c")
expect(app.get_by_role("dialog")).to_be_visible()
expect(app.get_by_role("dialog")).to_have_text(
"""
Clear cachesAre you sure you want to clear the app's function caches?This will remove all cached entries from functions using @st.cache_data and @st.cache_resource.CancelClear caches
"""
)
expect(app.get_by_role("dialog")).to_contain_text("Clear caches")


modifier_keys = ["Control", "Meta"]
Expand Down
18 changes: 9 additions & 9 deletions frontend/app/src/components/StreamlitDialog/StreamlitDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
import { STREAMLIT_HOME_URL } from "@streamlit/app/src/urls"
import {
StyledCode,
StyledInlineCode,
StyledPre,
} from "@streamlit/lib/src/components/elements/CodeBlock/styled-components"

Expand Down Expand Up @@ -190,19 +189,20 @@ interface ClearCacheProps {
* onClose - callback to close the dialog
*/
function clearCacheDialog(props: ClearCacheProps): ReactElement {
// Markdown New line is 2 spaces + \n
const newLineMarkdown = " \n"
const clearCacheInfo = [
`**Are you sure you want to clear the app's function caches?**`,
"This will remove all cached entries from functions using",
"`@st.cache_data` and `@st.cache_resource`.",
].join(newLineMarkdown)

return (
<div data-testid="stClearCacheDialog">
<Modal isOpen onClose={props.onClose}>
<ModalHeader>Clear caches</ModalHeader>
<ModalBody>
<div>
<b>Are you sure you want to clear the app's function caches?</b>
</div>
<div>
This will remove all cached entries from functions using{" "}
<StyledInlineCode>@st.cache_data</StyledInlineCode> and{" "}
<StyledInlineCode>@st.cache_resource</StyledInlineCode>.
</div>
<StreamlitMarkdown source={clearCacheInfo} allowHTML={false} />
</ModalBody>
<ModalFooter>
<ModalButton kind={BaseButtonKind.TERTIARY} onClick={props.onClose}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ export function ArrowTable(props: Readonly<TableProps>): ReactElement {
<StyledTableContainer className="stTable" data-testid="stTable">
{cssStyles && <style>{cssStyles}</style>}
<StyledTable id={cssId} data-testid="stTableStyledTable">
{caption && (
<caption>
<small>{caption}</small>
</caption>
)}
{caption && <caption>{caption}</caption>}
{columnHeaders.length > 0 && (
<thead>
{columnHeaders.map(rowIndex =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ export const StyledTable = styled.table(({ theme }) => ({
captionSide: "bottom",
border: `${theme.sizes.borderWidth} solid ${theme.colors.borderColorLight}`,
"& caption": {
fontFamily: theme.genericFonts.bodyFont,
fontSize: theme.fontSizes.sm,
paddingTop: theme.spacing.sm,
paddingBottom: 0,
color: theme.colors.fadedText60,
textAlign: "left",
wordWrap: "break-word",
},
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ export const StyledMessageWrapper = styled.div(({ theme }) => ({
minHeight: "100%",
fontSize: theme.fontSizes.sm,
lineHeight: theme.lineHeights.base,
div: {
display: "inline-flex",
},
}))
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import AlertContainer, {
} from "@streamlit/lib/src/components/shared/AlertContainer"
import { StyledCode } from "@streamlit/lib/src/components/elements/CodeBlock/styled-components"

import { StyledStackTrace } from "./styled-components"
import { StyledErrorName, StyledStackTrace } from "./styled-components"

export interface ErrorElementProps {
name: string
Expand All @@ -47,7 +47,7 @@ function ErrorElement(props: ErrorElementProps): ReactElement {

return (
<AlertContainer kind={Kind.ERROR} width={width}>
<strong>{name}: </strong>
<StyledErrorName>{name}: </StyledErrorName>
{message}
{stack ? (
<StyledStackTrace data-testid="stErrorElementStack">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ export const StyledStackTrace = styled.pre(({ theme }) => ({
padding: theme.spacing.lg,
border: `${theme.sizes.borderWidth} solid ${theme.colors.borderColor}`,
}))

export const StyledErrorName = styled.strong(({ theme }) => ({
fontWeight: theme.fontWeights.bold,
}))
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ describe("StreamlitMarkdown", () => {
render(<StreamlitMarkdown source={source} allowHTML={false} isToast />)

const textTag = screen.getByText("Here is some toast text")
expect(textTag).toHaveStyle("font-size: 14px")
expect(textTag).toBeInTheDocument()

// Use the smaller font size for the markdown container
const markdownContainer = screen.getByTestId("stMarkdownContainer")
expect(markdownContainer).toHaveStyle("font-size: 14px")
})

it("renders regular text sizing when largerLabel is true", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,15 @@ export const StyledStreamlitMarkdown =
}) => {
// Widget Labels have smaller font size with exception of Button/Checkbox/Radio Button labels
// Toasts also have smaller font size
const labelFontSize = (isLabel && !largerLabel) || isToast
const useSmallerFontSize =
(isLabel && !largerLabel) || isToast || isCaption

return {
fontFamily: theme.genericFonts.bodyFont,
fontSize: useSmallerFontSize ? theme.fontSizes.sm : theme.fontSizes.md,
marginBottom: isLabel ? "" : `-${theme.spacing.lg}`,
opacity: isCaption ? 0.6 : undefined,
color: "inherit",
...sharedMarkdownStyle(theme),
...getMarkdownHeadingDefinitions(
theme,
Expand All @@ -181,7 +186,6 @@ export const StyledStreamlitMarkdown =
marginTop: theme.spacing.none,
marginLeft: theme.spacing.none,
marginRight: theme.spacing.none,
...(labelFontSize ? { fontSize: theme.fontSizes.sm } : {}),
},

img: {
Expand All @@ -199,7 +203,6 @@ export const StyledStreamlitMarkdown =
// But this would require some styling changes.
margin: "0.2em 0 0.2em 1.2em",
padding: "0 0 0 0.6em",
fontSize: theme.fontSizes.md,
},

// Handles quotes:
Expand All @@ -209,6 +212,10 @@ export const StyledStreamlitMarkdown =
borderLeft: `${theme.sizes.borderWidth} solid ${theme.colors.lightGray}`,
},

"b, strong": {
fontWeight: theme.fontWeights.bold,
},

// Handles the horizontal divider:
hr: {
margin: "2em 0",
Expand Down Expand Up @@ -251,25 +258,9 @@ export const StyledStreamlitMarkdown =
borderRadius: theme.radii.md,
},

...(isToast
? {
div: {
display: "inline-flex",
},
}
: {}),

...(isCaption
? {
color: isInSidebarOrDialog
? theme.colors.gray
: theme.colors.fadedText60,
fontSize: theme.fontSizes.sm,
"p, ol, ul, dl, li": {
fontSize: "inherit",
},
}
: {}),
"p, ol, ul, dl, li": {
fontSize: "inherit",
},
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ describe("Widget Label", () => {
render(<WidgetLabel {...props} />)

expect(screen.getByTestId("stWidgetLabel")).toBeInTheDocument()
// Test that isLabel prop is true, which makes font size smaller
expect(screen.getByText(`${props.label}`)).toHaveStyle(`font-size: 14px`)

// Use the smaller font size for the markdown container
const markdownContainer = screen.getByTestId("stMarkdownContainer")
expect(markdownContainer).toHaveStyle("font-size: 14px")
})

it("can be disabled", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe("Dataframe Tooltip", () => {
const tooltipContent = screen.getByText("This is a tooltip.")
expect(tooltipContent).toBeInTheDocument()
// Uses markdown to render the content:
expect(tooltipContent).toHaveStyle("font-weight: bold")
expect(tooltipContent).toHaveStyle("font-weight: 600")
})

test("renders the tooltip at the correct position", () => {
Expand Down
15 changes: 0 additions & 15 deletions frontend/lib/src/theme/globalStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,6 @@ export const globalStyles = (theme: EmotionTheme): SerializedStyles => css`
margin-bottom: 0;
}
// Strong
//
// Add the correct font weight in Chrome, Edge, and Safari
b,
strong {
font-weight: ${theme.fontWeights.bold};
}
// Small
small {
font-size: ${theme.fontSizes.sm};
}
// And undo these styles for placeholder links/named anchors (without href).
// It would be more straightforward to just use a[href] in previous block, but that
// causes specificity issues in many other styles that are too complex to fix.
Expand Down

0 comments on commit dfa5f37

Please sign in to comment.