Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ You can use **any** array of blocks as `flex-layout.row` and `flex-layout.col` c

The props below support [`responsive-values`](https://github.com/vtex-apps/responsive-values), meaning that you can define to the same prop different values based on each device's screen size, such as mobile and desktop.

For example:
```
{
"flex-layout.row#row": {
"props": {
"marginBottom": {
"mobile": 2,
"desktop": 6
},
"width": {
"desktop": "33%",
"tablet": "50%",
"phone": "100%"
}
},
"children": [
"flex-layout.col#col1",
"flex-layout.col#col2",
...
]
}
```

### `flex-layout.row`

| Prop name | Type | Description | Default value |
Expand Down
3 changes: 2 additions & 1 deletion react/hooks/responsiveWidth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const useResponsiveWidth = (
const { device } = useDevice()

const isPhone = device === 'phone'
const isTablet = device === 'tablet'

const { preserveLayoutOnMobile = false, hideEmptyCols = false } =
options || {}
Expand All @@ -102,7 +103,7 @@ export const useResponsiveWidth = (
if (width && typeof width === 'object') {
return {
element: col,
width: isPhone ? width.mobile || 0 : width.desktop || 0,
width: isPhone ? width.phone || 0 : isTablet ? width.tablet || 0 : width.desktop || 0,
hasDefinedWidth: true,
isResponsive: true,
}
Expand Down
8 changes: 6 additions & 2 deletions react/modules/valuesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ type Group<T, U> = { [key in keyof T]: U }
type TachyonsInputGroup<T> = Group<T, TachyonsScaleInput>
interface ResponsiveInput<T> {
mobile: T
phone: T
tablet: T
desktop: T
}

const MAX_TACHYONS_SCALE = 11

//eslint-disable-next-line @typescript-eslint/no-explicit-any
const isResponsiveInput = <T>(value: any): value is ResponsiveInput<T> =>
value && value.mobile != null && value.desktop != null
value && (value.mobile != null || value.phone != null || value.tablet != null) && value.desktop != null

/** Takes a parser of units, and returns a parser that accepts either a
* value or a responsive input of that same type of value
Expand All @@ -21,10 +23,12 @@ const isResponsiveInput = <T>(value: any): value is ResponsiveInput<T> =>
*/
export const parseResponsive = <T, U>(parse: (value: T) => U) => (
value: T | ResponsiveInput<T>
): null | U | { mobile: U; desktop: U } => {
): null | U | { mobile: U; phone: U; tablet: U; desktop: U } => {
if (isResponsiveInput(value)) {
return {
mobile: parse(value.mobile),
phone: parse(value.phone),
tablet: parse(value.tablet),
desktop: parse(value.desktop),
}
}
Expand Down