Skip to content
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

TypeScript: pageCanvas provider #12891

Merged
merged 16 commits into from
Dec 30, 2022
16 changes: 2 additions & 14 deletions packages/design-system/src/theme/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@
/**
* Internal dependencies
*/
import { dark as darkMode, light as lightMode } from './colors';
export * from './theme';
import { light as lightMode } from './colors';
import { THEME_CONSTANTS, BEZIER } from './constants';
import * as ThemeGlobals from './global';
import * as themeHelpers from './helpers';
import { typography } from './typography';
import { borders } from './borders';
import { breakpoint, raw } from './breakpoint';

export const theme = {
borders,
typography,
colors: { ...darkMode },
breakpoint: {
...breakpoint,
raw,
},
};

export { lightMode, THEME_CONSTANTS, themeHelpers, ThemeGlobals, BEZIER };
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import { useContextSelector, identity } from '@googleforcreators/react';

/**
* Internal dependencies
*/
import Context from './context';

function usePageCanvas(selector) {
return useContextSelector(Context, selector ?? identity);
}

export default usePageCanvas;
import { dark as darkMode } from './colors';
import { typography } from './typography';
import { borders } from './borders';
import { breakpoint, raw } from './breakpoint';
export const theme = {
borders,
typography,
colors: { ...darkMode },
breakpoint: {
...breakpoint,
raw,
},
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted the theme part to TypeScript since it seems to be constants only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks git for thinking this is the same file before and after 🙄

1 change: 1 addition & 0 deletions packages/design-system/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export * from './types/keyboard';
export * from './utils/constants';
export * from './utils/localStore';
export * from './utils/sessionStore';
export * from './theme/theme';

export {};
1 change: 1 addition & 0 deletions packages/design-system/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"src/components/snackbar/constants.ts",
"src/contexts/snackbar/*",
"src/theme/constants",
"src/theme/*.ts",
"src/types/*",
"src/types.ts",
"src/utils/*.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
/**
* External dependencies
*/
import {
BACKGROUND_TEXT_MODE,
StoryPropTypes,
} from '@googleforcreators/elements';
import { BACKGROUND_TEXT_MODE } from '@googleforcreators/elements';
import {
createSolid,
generatePatternStyles,
} from '@googleforcreators/patterns';
import { useMemo } from '@googleforcreators/react';
import { getHTMLFormatters } from '@googleforcreators/rich-text';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import type { TextElement } from '@googleforcreators/elements';
import type { CSSProperties } from 'react';

/**
* Internal dependencies
Expand All @@ -39,18 +37,19 @@ import {
calcFontMetrics,
} from './util';

type DataToStyle = (prop: number) => string;
interface TextOutputWithUnitsProps {
element: TextElement;
dataToStyleX: DataToStyle;
dataToStyleY: DataToStyle;
dataToFontSizeY?: DataToStyle;
dataToPaddingX?: DataToStyle;
dataToPaddingY?: DataToStyle;
className?: string;
}

/**
* Renders DOM for the text output based on the provided unit converters.
*
* @param {Object<*>} props Component props.
* @param {Object<*>} props.element Story element.
* @param {Function} props.dataToStyleX dataToStyleX function.
* @param {Function} props.dataToStyleY dataToStyleY function.
* @param {Function} props.dataToFontSizeY dataToFontSizeY function. Falls back to dataToStyleY if not provided.
* @param {Function} props.dataToPaddingX dataToPaddingX function. Falls back to dataToStyleX if not provided.
* @param {Function} props.dataToPaddingY dataToPaddingY function. Falls back to dataToStyleX if not provided.
* @param {string} props.className Class name.
* @return {*} Rendered component.
*/
function TextOutputWithUnits({
element,
Expand All @@ -60,7 +59,7 @@ function TextOutputWithUnits({
dataToPaddingX,
dataToPaddingY,
className,
}) {
}: TextOutputWithUnitsProps) {
const {
content: rawContent,
backgroundColor,
Expand Down Expand Up @@ -132,24 +131,24 @@ function TextOutputWithUnits({
background: 'none',
lineHeight,
overflowWrap: 'break-word',
};
} as CSSProperties;

const highlightCloneStyle = {
...highlightStyle,
position: 'absolute',
top: 0,
left: 0,
right: 0,
};
} as CSSProperties;

const marginStyle = (el) => {
const marginStyle = (el: TextElement): CSSProperties => {
const { marginOffset } = calcFontMetrics(el);
return {
display: 'block',
position: 'relative',
left: 0,
top: '0',
margin: `${dataToPaddingY(-marginOffset / 2)} 0`,
margin: `${dataToPaddingY ? dataToPaddingY(-marginOffset / 2) : 0} 0`,
/* stylelint-disable-next-line */
WebkitBoxDecorationBreak: 'clone',
boxDecorationBreak: 'clone',
Expand All @@ -167,7 +166,7 @@ function TextOutputWithUnits({
borderRadius: `${borderRadius?.topLeft || 0}px ${
borderRadius?.topRight || 0
}px ${borderRadius?.bottomRight || 0}px ${borderRadius?.bottomLeft || 0}px`,
};
} as CSSProperties;

const backgroundTextStyle = {
...textStyle,
Expand Down Expand Up @@ -226,14 +225,4 @@ function TextOutputWithUnits({
);
}

TextOutputWithUnits.propTypes = {
element: StoryPropTypes.textContent.isRequired,
dataToStyleX: PropTypes.func.isRequired,
dataToStyleY: PropTypes.func.isRequired,
dataToFontSizeY: PropTypes.func,
dataToPaddingX: PropTypes.func,
dataToPaddingY: PropTypes.func,
className: PropTypes.string,
};

export default TextOutputWithUnits;
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,39 @@
* limitations under the License.
*/

/**
* External dependencies
*/
import type { CSSProperties } from 'react';
import type {
Padding,
TextAlign,
TextElement,
TextElementFont,
} from '@googleforcreators/elements';

type DataToStyle = (prop: number) => string;
interface Props {
font: TextElementFont;
fontSize: number;
lineHeight: number;
padding?: Padding;
textAlign: TextAlign;
}
/**
* Generates paragraph text style for a text element.
*
* @param {Object} props Props.
* @param {function(number):any} dataToStyleX Converts a x-unit to CSS.
* @param {function(number):any} dataToStyleY Converts a y-unit to CSS.
* @param {function(number):any} dataToFontSizeY Converts a font-size metric to
* y-unit CSS.
* @param {Object<*>} element Text element properties.
* @param {function(number):any} dataToPaddingY Falls back to dataToStyleX if not provided.
* @return {Object} The map of text style properties and values.
*/
export function generateParagraphTextStyle(
props,
dataToStyleX,
dataToStyleY,
props: Props,
dataToStyleX: DataToStyle,
dataToStyleY: DataToStyle,
dataToFontSizeY = dataToStyleY,
element,
element: TextElement,
dataToPaddingY = dataToStyleY
) {
): Omit<CSSProperties, 'font'> & {
dataToEditorY: DataToStyle;
font: TextElementFont;
} {
const { font, fontSize, lineHeight, padding, textAlign } = props;
const { marginOffset } = calcFontMetrics(element);

Expand All @@ -59,7 +72,14 @@ export function generateParagraphTextStyle(
};
}

export const generateFontFamily = ({ family, fallbacks } = {}) => {
interface GenerateFontFamilyProps {
family?: string;
fallbacks?: string[];
}
export const generateFontFamily = ({
family,
fallbacks,
}: GenerateFontFamilyProps = {}) => {
const genericFamilyKeywords = [
'cursive',
'fantasy',
Expand All @@ -80,11 +100,11 @@ export const generateFontFamily = ({ family, fallbacks } = {}) => {
return fontFamilyDisplay;
};

export const getHighlightLineheight = function (
lineHeight,
export const getHighlightLineheight = (
lineHeight: number,
verticalPadding = 0,
unit = 'px'
) {
) => {
if (verticalPadding === 0) {
return `${lineHeight}em`;
}
Expand All @@ -93,7 +113,7 @@ export const getHighlightLineheight = function (
}${unit})`;
};

export function calcFontMetrics(element) {
export function calcFontMetrics(element: TextElement) {
if (!element.font?.metrics) {
return {
contentAreaPx: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/element-library/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
// delete this file once complete.

export * from './constants';
export * from './utils/textMeasurements';

export {};