-
Notifications
You must be signed in to change notification settings - Fork 4.6k
🪟🧹 Introduce flex component #20944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🪟🧹 Introduce flex component #20944
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ComponentStory, ComponentMeta } from "@storybook/react"; | ||
|
||
import { FlexContainer } from "./FlexContainer"; | ||
import { FlexItem } from "./FlexItem"; | ||
|
||
export default { | ||
title: "Ui/Flex", | ||
component: FlexContainer, | ||
} as ComponentMeta<typeof FlexContainer>; | ||
|
||
const Template: ComponentStory<typeof FlexContainer> = (args) => <FlexContainer {...args} />; | ||
|
||
const Item = ({ label, grow }: { label: string; grow?: boolean }) => ( | ||
<FlexItem grow={grow} style={{ backgroundColor: "#dddddd", borderRadius: 5, padding: 20 }}>{`<FlexItem${ | ||
grow ? " grow" : "" | ||
}>${label}</FlexItem>`}</FlexItem> | ||
); | ||
|
||
export const NoGrow = Template.bind({}); | ||
NoGrow.args = { | ||
children: ( | ||
<> | ||
<Item label="first" /> | ||
<Item label="second with a lot of content" /> | ||
<Item label="third" /> | ||
<Item label="forth" /> | ||
</> | ||
), | ||
}; | ||
|
||
export const Grow = Template.bind({}); | ||
Grow.args = { | ||
children: ( | ||
<> | ||
<Item label="first" grow /> | ||
<Item label="second" /> | ||
</> | ||
), | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
@use "scss/variables"; | ||
|
||
.container { | ||
display: flex; | ||
} | ||
|
||
.directionRow { | ||
flex-direction: row; | ||
} | ||
|
||
.directionColumn { | ||
flex-direction: column; | ||
} | ||
|
||
.directionColumnReverse { | ||
flex-direction: column-reverse; | ||
} | ||
|
||
.directionRowReverse { | ||
flex-direction: row-reverse; | ||
} | ||
|
||
.gapXs { | ||
gap: variables.$spacing-xs; | ||
} | ||
|
||
.gapSm { | ||
gap: variables.$spacing-sm; | ||
} | ||
|
||
.gapMd { | ||
gap: variables.$spacing-md; | ||
} | ||
|
||
.gapLg { | ||
gap: variables.$spacing-lg; | ||
} | ||
|
||
.gapXl { | ||
gap: variables.$spacing-xl; | ||
} | ||
|
||
.gap2xl { | ||
gap: variables.$spacing-2xl; | ||
} | ||
|
||
.alignItemsStart { | ||
align-items: flex-start; | ||
} | ||
|
||
.alignItemsEnd { | ||
align-items: flex-end; | ||
} | ||
|
||
.alignItemsCenter { | ||
align-items: center; | ||
} | ||
|
||
.alignItemsBaseline { | ||
align-items: baseline; | ||
} | ||
|
||
.alignItemsStretch { | ||
align-items: stretch; | ||
} | ||
|
||
.justifyContentStart { | ||
justify-content: flex-start; | ||
} | ||
|
||
.justifyContentEnd { | ||
justify-content: flex-end; | ||
} | ||
|
||
.justifyContentCenter { | ||
justify-content: center; | ||
} | ||
|
||
.justifyContentBetween { | ||
justify-content: space-between; | ||
} | ||
|
||
.justifyContentAround { | ||
justify-content: space-around; | ||
} | ||
|
||
.justifyContentEvenly { | ||
justify-content: space-evenly; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import classNames from "classnames"; | ||
import React, { HTMLAttributes } from "react"; | ||
|
||
import styles from "./FlexContainer.module.scss"; | ||
|
||
interface FlexContainerProps { | ||
className?: string; | ||
/** | ||
* The flex-direction css property | ||
*/ | ||
direction?: "row" | "column" | "row-reverse" | "column-reverse"; | ||
krishnaglick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* gap between the flex items - defaults to `md` if not provided. None means no gap is applied, the others map to the respective scss spacing variable. | ||
*/ | ||
gap?: "none" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl"; | ||
/** | ||
* The align-items css property | ||
*/ | ||
alignItems?: "flex-start" | "flex-end" | "center" | "baseline" | "stretch"; | ||
/** | ||
* The justify-content css property | ||
*/ | ||
justifyContent?: "flex-start" | "flex-end" | "center" | "space-between" | "space-around" | "space-evenly"; | ||
} | ||
|
||
/** | ||
* Renders a div element which layouts its children as flex items as specified by the props. | ||
* Children of a `FlexContainer` can but don't have to be `FlexItem` elements. | ||
*/ | ||
export const FlexContainer: React.FC<React.PropsWithChildren<FlexContainerProps & HTMLAttributes<HTMLDivElement>>> = ({ | ||
className, | ||
direction = "row", | ||
gap = "md", | ||
alignItems = "stretch", | ||
justifyContent = "flex-start", | ||
children, | ||
...otherProps | ||
}) => { | ||
const fullClassName = classNames( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a benefit to memoizing the className generation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As it depends on so many props my feeling was that the dirty-check would take longer than re-evaluating the className. |
||
{ | ||
[styles.directionRow]: direction === "row", | ||
[styles.directionColumn]: direction === "column", | ||
[styles.directionRowReverse]: direction === "row-reverse", | ||
[styles.directionColumnReverse]: direction === "column-reverse", | ||
[styles.gapXs]: gap === "xs", | ||
[styles.gapSm]: gap === "sm", | ||
[styles.gapMd]: gap === "md", | ||
[styles.gapLg]: gap === "lg", | ||
[styles.gapXl]: gap === "xl", | ||
[styles.gap2xl]: gap === "2xl", | ||
[styles.alignItemsStart]: alignItems === "flex-start", | ||
[styles.alignItemsEnd]: alignItems === "flex-end", | ||
[styles.alignItemsCenter]: alignItems === "center", | ||
[styles.alignItemsBaseline]: alignItems === "baseline", | ||
[styles.alignItemsStretch]: alignItems === "stretch", | ||
[styles.justifyContentStart]: justifyContent === "flex-start", | ||
[styles.justifyContentEnd]: justifyContent === "flex-end", | ||
[styles.justifyContentCenter]: justifyContent === "center", | ||
[styles.justifyContentBetween]: justifyContent === "space-between", | ||
[styles.justifyContentAround]: justifyContent === "space-around", | ||
[styles.justifyContentEvenly]: justifyContent === "space-evenly", | ||
}, | ||
styles.container, | ||
className | ||
); | ||
|
||
return ( | ||
<div className={fullClassName} {...otherProps}> | ||
{children} | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@use "scss/variables"; | ||
|
||
.alignSelfStart { | ||
align-items: flex-end; | ||
} | ||
|
||
.alignSelfEnd { | ||
align-items: flex-end; | ||
} | ||
|
||
.alignSelfCenter { | ||
align-items: center; | ||
} | ||
|
||
.alignSelfBaseline { | ||
align-items: baseline; | ||
} | ||
|
||
.alignSelfStretch { | ||
align-items: stretch; | ||
} | ||
|
||
.grow { | ||
flex-grow: 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import classNames from "classnames"; | ||
import React, { HTMLAttributes } from "react"; | ||
|
||
import styles from "./FlexItem.module.scss"; | ||
|
||
interface FlexItemProps { | ||
className?: string; | ||
/** | ||
* Sets `flex-grow` to 1 if truthy | ||
*/ | ||
grow?: boolean; | ||
/** | ||
* The `align-self` css property | ||
*/ | ||
alignSelf?: "flex-start" | "flex-end" | "center" | "baseline" | "stretch"; | ||
krishnaglick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Renders a div element which sets css properties for flex children as given by the props. | ||
* This component can be used within a `FlexContainer` parent if grow or self-align props should be set, but it can also be omitted | ||
* in case no special flex properties are required. | ||
*/ | ||
export const FlexItem: React.FC<React.PropsWithChildren<FlexItemProps & HTMLAttributes<HTMLDivElement>>> = ({ | ||
className, | ||
grow, | ||
alignSelf, | ||
children, | ||
...otherProps | ||
}) => { | ||
const fullClassName = classNames( | ||
{ | ||
[styles.grow]: grow, | ||
[styles.alignSelfStart]: alignSelf === "flex-start", | ||
[styles.alignSelfEnd]: alignSelf === "flex-end", | ||
[styles.alignSelfCenter]: alignSelf === "center", | ||
[styles.alignSelfBaseline]: alignSelf === "baseline", | ||
[styles.alignSelfStretch]: alignSelf === "stretch", | ||
}, | ||
className | ||
); | ||
|
||
return ( | ||
<div className={fullClassName} {...otherProps}> | ||
{children} | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { FlexContainer } from "./FlexContainer"; | ||
export { FlexItem } from "./FlexItem"; |
Uh oh!
There was an error while loading. Please reload this page.