-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app-page-builder): add ResponsiveElementsProvider to fix blocks p…
…review
- Loading branch information
Showing
9 changed files
with
106 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ge-builder/src/admin/components/ResponsiveElementsProvider/ResponsiveElementsProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useMemo } from "react"; | ||
import { Theme } from "@webiny/app-theme/types"; | ||
import { usePageBuilder } from "~/hooks/usePageBuilder"; | ||
import { mediaToContainer } from "./mediaToContainer"; | ||
import { PageElementsProvider } from "~/contexts/PageBuilder/PageElementsProvider"; | ||
import styled from "@emotion/styled"; | ||
|
||
const ResponsiveContainer = styled.div` | ||
container-type: inline-size; | ||
container-name: page-canvas; | ||
`; | ||
|
||
export const ResponsiveElementsProvider: React.FC = ({ children }) => { | ||
const pageBuilder = usePageBuilder(); | ||
|
||
// We override all `@media` usages in breakpoints with `@container page-canvas`. This is what | ||
// enables us responsive design inside the Page Builder's page editor. | ||
const containerizedTheme = useMemo(() => { | ||
const theme = pageBuilder.theme as Theme; | ||
|
||
// On a couple of occasions, we've seen the `theme` object being `null` for a brief moment. This | ||
// would happen when the theme is being loaded via a dynamic import, e.g. in a multi-theme setup. | ||
if (!theme) { | ||
return null; | ||
} | ||
|
||
return { | ||
...pageBuilder.theme, | ||
breakpoints: { | ||
...theme.breakpoints, | ||
...Object.keys(theme.breakpoints).reduce((result, breakpointName) => { | ||
const breakpoint = theme.breakpoints[breakpointName]; | ||
return { | ||
...result, | ||
[breakpointName]: mediaToContainer(breakpoint) | ||
}; | ||
}, {}) | ||
} | ||
} as Theme; | ||
}, [pageBuilder.theme]); | ||
|
||
return ( | ||
<ResponsiveContainer> | ||
<PageElementsProvider theme={containerizedTheme!}>{children}</PageElementsProvider> | ||
</ResponsiveContainer> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/app-page-builder/src/admin/components/ResponsiveElementsProvider/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./ResponsiveElementsProvider"; |
21 changes: 21 additions & 0 deletions
21
...ages/app-page-builder/src/admin/components/ResponsiveElementsProvider/mediaToContainer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const minWidthRegExp = new RegExp("min-width:[ ]*([0-9a-z]*)"); | ||
const maxWidthRegExp = new RegExp("max-width:[ ]*([0-9a-z]*)"); | ||
|
||
/** | ||
* At the moment, we only support basic transformations (check tests). | ||
* If this won't be good enoough, we can potentially check this | ||
* library: https://www.npmjs.com/package/media-query-parser | ||
*/ | ||
export const mediaToContainer = (mediaQuery: string): string => { | ||
const [, minWidth] = mediaQuery.match(minWidthRegExp) || []; | ||
const [, maxWidth] = mediaQuery.match(maxWidthRegExp) || []; | ||
|
||
const widthRules = [ | ||
minWidth && `(min-width: ${minWidth})`, | ||
maxWidth && `(max-width: ${maxWidth})` | ||
] | ||
.filter(Boolean) | ||
.join(" and "); | ||
|
||
return `@container page-canvas ${widthRules}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters