Skip to content

Commit

Permalink
feat: more advanced theme engine
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 16, 2018
1 parent ea3c731 commit 1df690a
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 85 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"mobx-react": "^4.3.3",
"openapi-sampler": "1.0.0-beta.9",
"perfect-scrollbar": "^1.3.0",
"polished": "^1.9.2",
"prismjs": "^1.8.1",
"prop-types": "^15.6.0",
"react-dropdown": "^1.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/common-elements/fields-layout.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import styled from '../styled-components';
import { transparentizeHex } from '../utils/styled';
import { transparentize } from 'polished';
import { deprecatedCss } from './mixins';

export const PropertiesTableCaption = styled.caption`
text-align: right;
font-size: 0.9em;
font-weight: normal;
color: ${props => transparentizeHex(props.theme.colors.text, 0.4)};
color: ${props => transparentize(0.4, props.theme.colors.text)};
`;

export const PropertyCell = styled.td`
Expand Down
18 changes: 9 additions & 9 deletions src/common-elements/fields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from 'styled-components';
import { transparentizeHex } from '../utils/styled';
import { PropertyNameCell } from './fields-layout';
import { transparentize } from 'polished';

export const ClickablePropertyNameCell = PropertyNameCell.extend`
cursor: pointer;
Expand All @@ -13,14 +13,14 @@ export const FieldLabel = styled.span`
`;

export const TypePrefix = styled(FieldLabel)`
color: ${props => transparentizeHex(props.theme.colors.text, 0.4)};
color: ${props => transparentize(0.4, props.theme.colors.text)};
`;

export const TypeName = styled(FieldLabel)`
color: ${props => transparentizeHex(props.theme.colors.text, 0.8)};
color: ${props => transparentize(0.8, props.theme.colors.text)};
`;
export const TypeTitle = styled(FieldLabel)`
color: ${props => transparentizeHex(props.theme.colors.text, 0.5)};
color: ${props => transparentize(0.5, props.theme.colors.text)};
`;

export const TypeFormat = TypeName;
Expand Down Expand Up @@ -55,13 +55,13 @@ export const PatternLabel = styled(FieldLabel)`

export const ExampleValue = styled.span`
font-family: ${props => props.theme.code.fontFamily};
background-color: ${props => transparentizeHex(props.theme.colors.text, 0.02)};
border: 1px solid ${props => transparentizeHex(props.theme.colors.text, 0.15)};
background-color: ${props => transparentize(0.02, props.theme.colors.text)};
border: 1px solid ${props => transparentize(0.15, props.theme.colors.text)};
margin: 0 3px;
padding: 0.4em 0.2em 0.2em;
font-size: 0.8em;
border-radius: 2px;
color: ${props => transparentizeHex(props.theme.colors.text, 0.9)};
color: ${props => transparentize(0.9, props.theme.colors.text)};
display: inline-block;
min-width: 20px;
text-align: center;
Expand All @@ -70,8 +70,8 @@ export const ExampleValue = styled.span`
`;

export const ConstraintItem = styled(FieldLabel)`
background-color: ${props => transparentizeHex(props.theme.colors.main, 0.15)};
color: ${props => transparentizeHex(props.theme.colors.main, 0.6)};
background-color: ${props => transparentize(0.15, props.theme.colors.main)};
color: ${props => transparentize(0.6, props.theme.colors.main)};
margin-right: 6px;
margin-left: 6px;
border-radius: 2px;
Expand Down
11 changes: 0 additions & 11 deletions src/common-elements/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,3 @@ export const deprecatedCss = css`
text-decoration: line-through;
color: #bdccd3;
`;

export const hoverColor = color => {
if (!color) {
return '';
}
return css`
&:hover {
color: ${color};
}
`;
};
6 changes: 3 additions & 3 deletions src/common-elements/panels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled, { media } from '../styled-components';

export const MiddlePanel = styled.div`
width: ${props => 100 - props.theme.rightPanel.width}%;
width: calc(100% - ${props => props.theme.rightPanel.width});
padding: ${props => props.theme.spacingUnit * 2}px;
${media.lessThan('medium')`
Expand All @@ -10,9 +10,9 @@ export const MiddlePanel = styled.div`
`;

export const RightPanel = styled.div`
width: ${props => props.theme.rightPanel.width}%;
width: ${props => props.theme.rightPanel.width};
color: #fafbfc;
bckground-color: ${props => props.theme.rightPanel.backgroundColor};
background-color: ${props => props.theme.rightPanel.backgroundColor};
padding: ${props => props.theme.spacingUnit * 2}px;
${media.lessThan('medium')`
Expand Down
12 changes: 9 additions & 3 deletions src/components/Redoc/elements.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { hoverColor } from '../../common-elements/mixins';
import styled, { media } from '../../styled-components';
export { ClassAttributes } from 'react';

Expand Down Expand Up @@ -29,8 +28,15 @@ export const RedocWrap = styled.div`
a {
text-decoration: none;
color: ${props => props.theme.links.color || props.theme.colors.main};
${props => hoverColor(props.theme.links.hover)};
color: ${props => props.theme.links.color};
&:visited {
color: ${props => props.theme.links.visited};
}
&:hover {
color: ${props => props.theme.links.hover};
}
}
`;

Expand Down
4 changes: 2 additions & 2 deletions src/components/Responses/styled.elements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from '../../styled-components';

import { UnderlinedHeader } from '../../common-elements';
import { transparentizeHex } from '../../utils';
import { transparentize } from 'polished';
import { ResponseTitle } from './ResponseTitle';

export const StyledResponseTitle = styled(ResponseTitle)`
Expand All @@ -13,7 +13,7 @@ export const StyledResponseTitle = styled(ResponseTitle)`
cursor: pointer;
color: ${props => props.theme.colors[props.type]};
background-color: ${props => transparentizeHex(props.theme.colors[props.type], 0.08)};
background-color: ${props => transparentize(0.08, props.theme.colors[props.type])};
${props =>
(props.empty &&
Expand Down
4 changes: 2 additions & 2 deletions src/components/SecurityRequirement/SecuirityRequirement.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as React from 'react';
import styled from '../../styled-components';
import { transparentizeHex } from '../../utils/styled';
import { transparentize } from 'polished';

import { UnderlinedHeader } from '../../common-elements/headers';
import { SecurityRequirementModel } from '../../services/models/SecurityRequirement';

const ScopeName = styled.code`
font-size: ${props => props.theme.code.fontSize};
font-family: ${props => props.theme.code.fontFamily};
border: 1px solid ${props => transparentizeHex(props.theme.colors.text, 0.15)};
border: 1px solid ${props => transparentize(0.15, props.theme.colors.text)};
margin: 0 3px;
padding: 0.2em;
display: inline-block;
Expand Down
6 changes: 3 additions & 3 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import defaultTheme, { ThemeInterface } from '../theme';
import defaultTheme, { ResolvedThemeInterface, ThemeInterface, resolveTheme } from '../theme';
import { querySelector } from '../utils/dom';
import { isNumeric, mergeObjects } from '../utils/helpers';

Expand Down Expand Up @@ -81,7 +81,7 @@ export class RedocNormalizedOptions {
return () => 0;
}

theme: ThemeInterface;
theme: ResolvedThemeInterface;
scrollYOffset: () => number;
hideHostname: boolean;
expandResponses: { [code: string]: boolean } | 'all';
Expand All @@ -93,7 +93,7 @@ export class RedocNormalizedOptions {
hideDownloadButton: boolean;

constructor(raw: RedocRawOptions) {
this.theme = mergeObjects({} as any, defaultTheme, raw.theme || {});
this.theme = resolveTheme(mergeObjects({} as any, defaultTheme, raw.theme || {}));
this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
this.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname);
this.expandResponses = RedocNormalizedOptions.normalizeExpandResponses(raw.expandResponses);
Expand Down
12 changes: 6 additions & 6 deletions src/styled-components.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as styledComponents from 'styled-components';

import { ThemeInterface } from './theme';
import { ResolvedThemeInterface } from './theme';

export type StyledFunction<T> = styledComponents.ThemedStyledFunction<T, ThemeInterface>;
export type StyledFunction<T> = styledComponents.ThemedStyledFunction<T, ResolvedThemeInterface>;

function withProps<T, U extends HTMLElement = HTMLElement>(
styledFunction: StyledFunction<React.HTMLProps<U>>,
Expand All @@ -19,7 +19,7 @@ const {
withTheme,
} = (styledComponents as styledComponents.ThemedStyledComponentsModule<
any
>) as styledComponents.ThemedStyledComponentsModule<ThemeInterface>;
>) as styledComponents.ThemedStyledComponentsModule<ResolvedThemeInterface>;

export const media = {
lessThan(breakpoint) {
Expand All @@ -40,9 +40,9 @@ export const media = {

between(firstBreakpoint, secondBreakpoint) {
return (...args) => css`
@media (min-width: ${props => props.theme.breakpoints[firstBreakpoint]}) and (max-width: ${props => props.theme.breakpoints[
secondBreakpoint
]}) {
@media (min-width: ${props =>
props.theme.breakpoints[firstBreakpoint]}) and (max-width: ${props =>
props.theme.breakpoints[secondBreakpoint]}) {
${(css as any)(...args)};
}
`;
Expand Down
121 changes: 113 additions & 8 deletions src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const theme = {
import { lighten } from 'polished';

const theme: ThemeInterface = {
spacingUnit: 20,
breakpoints: {
small: '50rem',
Expand All @@ -8,9 +10,9 @@ const theme = {
colors: {
main: '#32329f',
success: '#00aa13',
redirect: 'orange',
redirect: '#ffa500',
error: '#e53935',
info: 'skyblue',
info: '#87ceeb',
text: '#263238',
warning: '#f1c400',
http: {
Expand Down Expand Up @@ -44,9 +46,9 @@ const theme = {
fontFamily: 'Courier, monospace',
},
links: {
color: undefined, // by default main color
visited: undefined, // by default main color
hover: undefined, // by default main color
color: ({ colors }) => colors.main,
visited: ({ colors }) => colors.main,
hover: ({ colors }) => lighten(0.2, colors.main),
},
menu: {
width: '260px',
Expand All @@ -58,10 +60,113 @@ const theme = {
},
rightPanel: {
backgroundColor: '#263238',
width: 40,
width: '40%',
},
};

export default theme;

export type ThemeInterface = typeof theme;
export function resolveTheme(theme: ThemeInterface): ResolvedThemeInterface {
const resolvedValues = {};
let counter = 0;
const setProxy = (obj, path: string) => {
Object.keys(obj).forEach(k => {
const currentPath = (path ? path + '.' : '') + k;
const val = obj[k];
if (typeof val === 'function') {
Object.defineProperty(obj, k, {
get() {
if (!resolvedValues[currentPath]) {
counter++;
if (counter > 1000) {
throw new Error(
`Theme probably contains cirucal dependency at ${currentPath}: ${val.toString()}`,
);
}

resolvedValues[currentPath] = val(theme);
}
return resolvedValues[currentPath];
},
enumerable: true,
});
} else if (typeof val === 'object') {
setProxy(val, currentPath);
}
});
};

setProxy(theme, '');
return JSON.parse(JSON.stringify(theme));
}

export interface ResolvedThemeInterface {
spacingUnit: number;
breakpoints: {
small: string;
medium: string;
large: string;
};
colors: {
main: string;
success: string;
redirect: string;
error: string;
info: string;
text: string;
warning: string;
http: {
get: string;
post: string;
put: string;
options: string;
patch: string;
delete: string;
basic: string;
link: string;
};
};
schemaView: {
linesColor: string;
defaultDetailsWidth: string;
};
baseFont: {
size: string;
lineHeight: string;
weight: string;
family: string;
smoothing: string;
optimizeSpeed: boolean;
};
headingsFont: {
family: string;
};
code: {
fontSize: string;
fontFamily: string;
};
links: {
color: string;
visited: string;
hover: string;
};
menu: {
width: string;
backgroundColor: string;
};
logo: {
maxHeight: string;
width: string;
};
rightPanel: {
backgroundColor: string;
width: string;
};
}

export type primitive = string | number | boolean | undefined | null;
export type AdvancedThemeDeep<T> = T extends primitive
? T | ((theme: ResolvedThemeInterface) => T)
: AdvancedThemeObject<T>;
export type AdvancedThemeObject<T> = { [P in keyof T]: AdvancedThemeDeep<T[P]> };
export type ThemeInterface = AdvancedThemeObject<ResolvedThemeInterface>;
19 changes: 0 additions & 19 deletions src/utils/__tests__/styled.test.ts

This file was deleted.

Loading

0 comments on commit 1df690a

Please sign in to comment.