Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

TypeScript #1

Merged
merged 8 commits into from
Aug 21, 2019
Merged
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
3 changes: 0 additions & 3 deletions .storybook/.babelrc

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions .storybook/config.js → .storybook/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { configure } from "@storybook/react";

function loadStories() {
require("../packages/button/stories.jsx");
require("../packages/radio/stories.jsx");
require("../packages/button/stories.tsx");
require("../packages/radio/stories.tsx");
}

configure(loadStories, module);
17 changes: 17 additions & 0 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = ({ config, mode }) => {
//Get TypeScript working via babel preset
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve("babel-loader"),
options: {
presets: [
"@babel/preset-env",
"@babel/preset-typescript",
"@emotion/babel-preset-css-prop"
]
}
});
config.resolve.extensions.push(".ts", ".tsx");

return config;
};
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "source-components",
"version": "0.0.1",
"scripts": {
"storybook": "yarn watch:foundations & start-storybook",
"storybook": "yarn watch:foundations & yarn watch:svgs & start-storybook",
"build:storybook": "build-storybook -o dist",
"build:foundations": "cd packages/foundations && yarn build",
"watch:foundations": "cd packages/foundations && yarn watch",
"watch:svgs": "cd packages/svgs && yarn watch",
"build:svgs": "cd packages/svgs && yarn build",
"build:button": "cd packages/button && yarn build",
"build:radio": "cd packages/radio && yarn build",
Expand All @@ -19,14 +20,16 @@
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"@emotion/babel-preset-css-prop": "^10.0.14",
"@emotion/core": "^10.0.14",
"@storybook/addon-backgrounds": "^5.1.10",
"@storybook/react": "^5.1.9",
"@types/react": "^16.9.2",
"@types/storybook__react": "^4.0.2",
"babel-loader": "^8.0.6",
"gh-pages": "^2.0.1",
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"dependencies": {}
}
}
1 change: 1 addition & 0 deletions packages/button/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
presets: [
"@babel/preset-env",
"@babel/preset-typescript",
"@babel/preset-react",
["@emotion/babel-preset-css-prop", { sourceMap: false }]
]
Expand Down
9 changes: 7 additions & 2 deletions packages/button/button.jsx → packages/button/button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { SvgArrowRightStraight } from "@guardian/src-svgs/arrow-right-straight";
import React, { ReactElement, ReactNode } from "react";
import { SvgArrowRightStraight } from "@guardian/src-svgs";
import {
button,
primary,
Expand All @@ -23,6 +23,11 @@ const Button = ({
iconSide,
children,
...props
}: {
appearance: "primary" | "secondary";
icon: ReactElement;
iconSide: "left" | "right";
children: ReactNode;
}) => {
return (
<button
Expand Down
6 changes: 4 additions & 2 deletions packages/button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@emotion/babel-preset-css-prop": "^10.0.14",
"@guardian/src-svgs": "^0.0.1",
"rollup": "^1.17.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.2",
Expand All @@ -27,6 +27,8 @@
"react": "^16.8.6"
},
"dependencies": {
"@guardian/src-helpers": "^0.0.1"
"@guardian/src-foundations": "^0.0.5",
"@guardian/src-helpers": "^0.0.1",
"@guardian/src-svgs": "^0.0.2"
}
}
26 changes: 14 additions & 12 deletions packages/button/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import babel from "rollup-plugin-babel";
import resolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";

const extensions = [".ts", ".tsx"];

module.exports = {
input: "button.jsx",
output: {
file: "dist/button.js",
format: "cjs"
},
input: "button.tsx",
output: [
{
file: "dist/button.js",
format: "cjs"
},
{
file: "dist/button.esm.js",
format: "esm"
}
],
external: ["react", "@emotion/core", "@emotion/css"],
plugins: [
babel(),
resolve({
extensions: [".js", ".jsx"]
}),
commonjs()
]
plugins: [babel({ extensions }), resolve({ extensions }), commonjs()]
};
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/foundations/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
presets: ["@babel/preset-env"]
presets: ["@babel/preset-env", "@babel/preset-typescript"]
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const mediaQueryMinimums = {
wide: breakpoints[3]
};

const minWidth = value => `@media (min-width: ${value}px)`;
const maxWidth = value => `@media (max-width: ${value - 1}px)`;
const minWidthMaxWidth = (from, until) =>
const minWidth = (value: number) => `@media (min-width: ${value}px)`;
const maxWidth = (value: number) => `@media (max-width: ${value - 1}px)`;
const minWidthMaxWidth = (from: number, until: number) =>
`@media (min-width: ${`${from}px`}) and (max-width: ${`${until - 1}px`})`;

const mq = ({ from, until }) => {
Expand Down
25 changes: 14 additions & 11 deletions packages/foundations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
"name": "@guardian/src-foundations",
"version": "0.0.5",
"main": "dist/foundations.js",
"module": "dist/foundations.esm.js",
"scripts": {
"build": "rollup --config",
"watch": "rollup --config --watch",
"clean": "rm -rf dist",
"prepublish": "yarn build"
},
"files": [
"animation.js",
"helpers.js",
"index.js",
"media-queries.js",
"palette.js",
"size.js",
"space.js",
"theme.js",
"typography.js"
"animation.ts",
"helpers.ts",
"index.ts",
"media-queries.ts",
"palette.ts",
"size.ts",
"space.ts",
"theme.ts",
"typography.ts"
],
"dependencies": {
"devDependencies": {
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"rollup": "^1.19.4",
"rollup-plugin-babel": "^4.3.3"
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-node-resolve": "^5.2.0"
}
}
21 changes: 15 additions & 6 deletions packages/foundations/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import babel from "rollup-plugin-babel";
import resolve from "rollup-plugin-node-resolve";

const extensions = [".ts", ".tsx"];

module.exports = {
input: "index.js",
output: {
file: "dist/foundations.js",
format: "cjs"
},
plugins: [babel()]
input: "index.ts",
output: [
{
file: "dist/foundations.js",
format: "cjs"
},
{
file: "dist/foundations.esm.js",
format: "esm"
}
],
plugins: [babel({ extensions }), resolve({ extensions })]
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { fontSizes, fonts, lineHeights, fontWeights } from "./theme";

type Category = "headline" | "body" | "textSans";
type LineHeight = "tight" | "regular" | "loose";
type FontWeight = "light" | "regular" | "medium" | "bold";

const headlineSizes = {
1: fontSizes[2],
2: fontSizes[3],
Expand Down Expand Up @@ -29,25 +33,29 @@ const textSansSizes = {
10: fontSizes[9]
};

const fontSizeMapping = {
const fontSizeMapping: { [cat in Category]: { [level in number]: number } } = {
headline: headlineSizes,
body: bodySizes,
textSans: textSansSizes
};

const fontMapping = {
const fontMapping: { [cat in Category]: string } = {
headline: fonts.headlineSerif,
body: fonts.bodySerif,
textSans: fonts.bodySans
};

const lineHeightMapping = {
const lineHeightMapping: { [lineHight in LineHeight]: string } = {
tight: lineHeights[0],
regular: lineHeights[1],
loose: lineHeights[2]
};

const fontWeightMapping = {
const fontWeightMapping: {
[cat in Category]: {
[fontWeight in FontWeight]?: { fontWeight: number; hasItalic: boolean }
}
} = {
headline: {
light: {
fontWeight: fontWeights[0],
Expand Down Expand Up @@ -84,7 +92,17 @@ const fontWeightMapping = {
}
};

const fs = ({ category, level, lineHeight, fontWeight }) => {
const fs = ({
category,
level,
lineHeight,
fontWeight
}: {
category: Category;
level: number;
lineHeight: LineHeight;
fontWeight: FontWeight;
}) => {
// Gosh there's a lot of flaky logic here! We need to think about
// what happens if a value is not found. Maybe a big try / catch?
const fontFamilyValue = fontMapping[category];
Expand All @@ -100,13 +118,30 @@ const fs = ({ category, level, lineHeight, fontWeight }) => {
`;
};

const headline = ({ level, lineHeight = "tight", fontWeight = "medium" }) =>
interface FontScaleArgs {
level: number;
lineHeight?: LineHeight;
fontWeight?: FontWeight;
}

const headline = ({
level,
lineHeight = "tight",
fontWeight = "medium"
}: FontScaleArgs) =>
fs({ category: "headline", level, lineHeight, fontWeight });

const body = ({ level, lineHeight = "loose", fontWeight = "regular" }) =>
fs({ category: "body", level, lineHeight, fontWeight });
const body = ({
level,
lineHeight = "loose",
fontWeight = "regular"
}: FontScaleArgs) => fs({ category: "body", level, lineHeight, fontWeight });

const textSans = ({ level, lineHeight = "loose", fontWeight = "regular" }) =>
const textSans = ({
level,
lineHeight = "loose",
fontWeight = "regular"
}: FontScaleArgs) =>
fs({ category: "textSans", level, lineHeight, fontWeight });

Object.freeze(headlineSizes);
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions packages/radio/.babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript",
["@emotion/babel-preset-css-prop", { sourceMap: false }]
]
};
6 changes: 4 additions & 2 deletions packages/radio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@guardian/src-radio",
"version": "0.0.3",
"main": "dist/radio.js",
"module": "dist/radio.esm.js",
"scripts": {
"build": "rollup --config",
"clean": "rm -rf dist",
Expand All @@ -11,6 +12,7 @@
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@emotion/babel-preset-css-prop": "^10.0.14",
"@guardian/src-helpers": "^0.0.1",
"rollup": "^1.17.0",
Expand All @@ -19,8 +21,8 @@
"rollup-plugin-node-resolve": "^5.2.0"
},
"files": [
"radio.jsx",
"styles.js"
"radio.tsx",
"styles.ts"
],
"peerDependencies": {
"@emotion/core": "^10.0.14",
Expand Down
Loading