slate/components/core/FontFrame/index.js

120 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-03-23 17:54:30 +03:00
import * as React from "react";
import { css } from "@emotion/react";
import { useFont, useFontControls } from "./hooks";
2021-03-30 17:21:54 +03:00
import { Controls } from "./Settings/index";
import { FixedControls, MobileFixedControls } from "./Settings/FixedControls";
import FontView from "./Views/index";
2021-03-23 17:54:30 +03:00
2021-03-24 20:55:12 +03:00
const GET_STYLES_CONTAINER = (theme) => css`
2021-03-23 17:54:30 +03:00
position: relative;
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
2021-03-24 20:55:12 +03:00
background-color: ${theme.fontPreviewDarkMode ? theme.system.pitchBlack : theme.system.white};
padding-top: 14px;
2021-03-23 17:54:30 +03:00
`;
2021-03-30 17:21:54 +03:00
const STYLES_MOBILE_HIDDEN = (theme) => css`
@media (max-width: ${theme.sizes.mobile}px) {
display: none;
}
`;
const STYLES_MOBILE_ONLY = (theme) => css`
@media (min-width: ${theme.sizes.mobile}px) {
display: none;
}
`;
2021-03-23 17:54:30 +03:00
2021-03-30 17:21:54 +03:00
const STYLES_FONT_LOADER = (theme) => css`
display: flex;
justify-content: center;
align-items: center;
position: absolute;
width: 100%;
height: 100%;
color: ${theme.fontPreviewDarkMode ? "#fff" : "#000"};
background-color: ${theme.fontPreviewDarkMode ? "rgba(0,0,0,0.5)" : "rgba(255,255,255,0.8)"};
`;
const FontLoader = () => (
<div css={STYLES_FONT_LOADER}>
<p>loading...</p>
</div>
);
2021-03-23 17:54:30 +03:00
export default function FontFrame({ cid, url, ...props }) {
const { isFontLoading, fontName } = useFont({ url, name: cid }, [cid]);
2021-03-24 20:55:12 +03:00
const [
currentState,
{
2021-03-24 22:04:51 +03:00
updateView,
2021-03-24 20:55:12 +03:00
setLightMode,
setDarkMode,
toggleSettings,
updateFontSize,
updateLineHeight,
updateTracking,
updateColumn,
updateTextAlign,
updateVerticalAlign,
2021-03-30 17:21:54 +03:00
updateCustomView,
resetLayout,
getRandomLayout,
2021-03-24 20:55:12 +03:00
},
] = useFontControls();
2021-03-23 17:54:30 +03:00
return (
2021-03-24 20:55:12 +03:00
<div css={GET_STYLES_CONTAINER} style={{ fontFamily: fontName }} {...props}>
<div css={STYLES_MOBILE_HIDDEN}>
<FixedControls
onDarkMode={setDarkMode}
onLightMode={setLightMode}
onToggleSettings={toggleSettings}
isDarkMode={currentState.context.darkmode}
isSettingsVisible={currentState.context.showSettings}
/>
</div>
2021-03-24 22:04:51 +03:00
<div style={{ position: "relative", flexGrow: 1, overflowY: "scroll" }}>
2021-03-30 17:21:54 +03:00
{isFontLoading && <FontLoader />}
<FontView
view={currentState.view}
customView={currentState.customView}
2021-03-30 21:52:43 +03:00
shouldUpdateView={currentState.context.shouldUpdateView}
2021-03-30 17:21:54 +03:00
customViewContent={currentState.context.customViewContent}
settings={currentState.context.settings}
updateCustomView={updateCustomView}
/>
2021-03-24 20:55:12 +03:00
</div>
<div css={STYLES_MOBILE_HIDDEN}>
{currentState.context.showSettings && (
<Controls
view={currentState.view}
defaultOptions={currentState.defaultOptions}
resetLayout={resetLayout}
updateView={updateView}
settings={currentState.context.settings}
updateFontSize={updateFontSize}
updateLineHeight={updateLineHeight}
updateTracking={updateTracking}
updateColumn={updateColumn}
updateTextAlign={updateTextAlign}
updateVerticalAlign={updateVerticalAlign}
getRandomLayout={getRandomLayout}
/>
)}
</div>
<div css={STYLES_MOBILE_ONLY}>
<MobileFixedControls
2021-03-24 22:04:51 +03:00
view={currentState.view}
defaultOptions={currentState.defaultOptions}
2021-03-24 22:04:51 +03:00
updateView={updateView}
onDarkMode={setDarkMode}
onLightMode={setLightMode}
2021-03-24 22:04:51 +03:00
/>
</div>
2021-03-23 17:54:30 +03:00
</div>
);
}