Skip to content

Commit

Permalink
feat: theme hooks experimental hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Jun 29, 2018
1 parent b2c3c00 commit 55bd853
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 35 deletions.
3 changes: 3 additions & 0 deletions src/common-elements/fields-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { transparentize } from 'polished';
import * as React from 'react';

import styled, {
extensionsHook,
ResolvedThemeInterface,
StyledComponentClass,
withProps,
Expand Down Expand Up @@ -75,6 +76,8 @@ export const PropertyNameCell = withProps<{ kind?: string }>(PropertyCell.extend
}
${({ kind }) => (kind !== 'field' ? 'font-style: italic' : '')};
${extensionsHook('PropertyNameCell')};
`;

export const PropertyDetailsCell = styled.td`
Expand Down
21 changes: 20 additions & 1 deletion src/common-elements/headers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as React from 'react';
import { InterpolationFunction, Styles, ThemeProps } from 'styled-components';

import styled, { css, ResolvedThemeInterface, StyledComponentClass } from '../styled-components';
import styled, {
css,
extensionsHook,
ResolvedThemeInterface,
StyledComponentClass,
} from '../styled-components';

const headerFontSize = {
1: '1.85714em',
Expand All @@ -18,16 +23,28 @@ export const headerCommonMixin = level => css`
export const H1 = styled.h1`
${headerCommonMixin(1)};
color: ${props => props.theme.colors.main};
${extensionsHook('H1')};
`;

export const H2 = styled.h2`
${headerCommonMixin(2)};
color: black;
${extensionsHook('H2')};
`;

export const H3 = styled.h2`
${headerCommonMixin(3)};
color: black;
${extensionsHook('H3')};
`;

export const RightPanelHeader = styled.h3`
color: ${({ theme }) => theme.rightPanel.textColor};
${extensionsHook('RightPanelHeader')};
`;

export const UnderlinedHeader = styled.h5`
Expand All @@ -38,4 +55,6 @@ export const UnderlinedHeader = styled.h5`
text-transform: uppercase;
font-size: 0.929em;
line-height: 20px;
${extensionsHook('UnderlinedHeader')};
`;
32 changes: 17 additions & 15 deletions src/components/ApiInfo/ApiInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MiddlePanel, Row } from '../../common-elements/';
import { Markdown } from '../Markdown/Markdown';
import { SecurityDefs } from '../SecuritySchemes/SecuritySchemes';

import { StyledMarkdownBlock } from '../Markdown/styled.elements';
import {
ApiHeader,
DownloadButton,
Expand Down Expand Up @@ -77,22 +78,23 @@ export class ApiInfo extends React.Component<ApiInfoProps> {
</DownloadButton>
</p>
)}
<StyledMarkdownBlock>
{((info.license || info.contact || info.termsOfService) && (
<InfoSpanBoxWrap>
<InfoSpanBox>
{email} {website} {license} {terms}
</InfoSpanBox>
</InfoSpanBoxWrap>
)) ||
null}

{((info.license || info.contact || info.termsOfService) && (
<InfoSpanBoxWrap>
<InfoSpanBox>
{email} {website} {license} {terms}
</InfoSpanBox>
</InfoSpanBoxWrap>
)) ||
null}

{(externalDocs && (
<p>
<a href={externalDocs.url}>{externalDocs.description || externalDocs.url}</a>
</p>
)) ||
null}
{(externalDocs && (
<p>
<a href={externalDocs.url}>{externalDocs.description || externalDocs.url}</a>
</p>
)) ||
null}
</StyledMarkdownBlock>
</MiddlePanel>
</Row>
);
Expand Down
10 changes: 9 additions & 1 deletion src/components/ApiInfo/styled.elements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AnchorHTMLAttributes, ClassAttributes, HTMLAttributes } from 'react';

import { H1, MiddlePanel } from '../../common-elements';
import styled, { ResolvedThemeInterface, StyledComponentClass } from '../../styled-components';
import styled, {
extensionsHook,
ResolvedThemeInterface,
StyledComponentClass,
} from '../../styled-components';

const delimiterWidth = 15;

Expand All @@ -10,6 +14,8 @@ export const ApiInfoWrap = MiddlePanel;
export const ApiHeader = H1.extend`
margin-top: 0;
margin-bottom: 0.5em;
${extensionsHook('ApiHeader')};
`;

export const DownloadButton = styled.a`
Expand All @@ -20,6 +26,8 @@ export const DownloadButton = styled.a`
padding: 4px 8px 4px;
display: inline-block;
text-decoration: none;
${extensionsHook('DownloadButton')};
`;

export const InfoSpan = styled.span`
Expand Down
16 changes: 16 additions & 0 deletions src/components/Markdown/styled.elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InterpolationFunction, Styles, ThemeProps } from 'styled-components';
import { headerCommonMixin, linkifyMixin } from '../../common-elements';
import styled, {
css,
extensionsHook,
ResolvedThemeInterface,
StyledComponentClass,
withProps,
Expand Down Expand Up @@ -132,4 +133,19 @@ export const StyledMarkdownBlock = withProps<{ dense?: boolean; inline?: boolean
}
${linkifyMixin('.share-link')};
${extensionsHook('Markdown')};
a {
text-decoration: none;
color: ${props => props.theme.links.color};
&:visited {
color: ${props => props.theme.links.visited};
}
&:hover {
color: ${props => props.theme.links.hover};
}
}
`;
13 changes: 0 additions & 13 deletions src/components/Redoc/styled.elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ export const RedocWrap = styled.div`
.redoc-markdown h1 {
padding-top: ${props => props.theme.spacingUnit * 4}px;
}
a {
text-decoration: none;
color: ${props => props.theme.links.color};
&:visited {
color: ${props => props.theme.links.visited};
}
&:hover {
color: ${props => props.theme.links.hover};
}
}
`;

export const ApiContentWrap = styled.div`
Expand Down
4 changes: 2 additions & 2 deletions src/components/RequestSamples/RequestSamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { OperationModel } from '../../services/models';
import { PayloadSamples } from '../PayloadSamples/PayloadSamples';
import { SourceCodeWithCopy } from '../SourceCode/SourceCode';

import { Tab, TabList, TabPanel, Tabs } from '../../common-elements';
import { RightPanelHeader, Tab, TabList, TabPanel, Tabs } from '../../common-elements';

export interface RequestSamplesProps {
operation: OperationModel;
Expand All @@ -24,7 +24,7 @@ export class RequestSamples extends React.Component<RequestSamplesProps> {
return (
(hasSamples && (
<div>
<h3> Request samples </h3>
<RightPanelHeader> Request samples </RightPanelHeader>

<Tabs defaultIndex={0}>
<TabList>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ResponseSamples/ResponseSamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';

import { MediaContentModel, OperationModel } from '../../services/models';

import { Tab, TabList, TabPanel, Tabs } from '../../common-elements';
import { RightPanelHeader, Tab, TabList, TabPanel, Tabs } from '../../common-elements';
import { PayloadSamples } from '../PayloadSamples/PayloadSamples';

export interface ResponseSamplesProps {
Expand All @@ -23,7 +23,7 @@ export class ResponseSamples extends React.Component<ResponseSamplesProps> {
return (
(responses.length > 0 && (
<div>
<h3> Response samples </h3>
<RightPanelHeader> Response samples </RightPanelHeader>

<Tabs defaultIndex={0}>
<TabList>
Expand Down
7 changes: 7 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ export class RedocNormalizedOptions {
unstable_ignoreMimeParameters: boolean;

constructor(raw: RedocRawOptions) {
let hook;
if (raw.theme && raw.theme.extensionsHook) {
hook = raw.theme.extensionsHook;
raw.theme.extensionsHook = undefined;
}
this.theme = resolveTheme(mergeObjects({} as any, defaultTheme, raw.theme || {}));
this.theme.extensionsHook = hook;

this.scrollYOffset = RedocNormalizedOptions.normalizeScrollYOffset(raw.scrollYOffset);
this.hideHostname = RedocNormalizedOptions.normalizeHideHostname(raw.hideHostname);
this.expandResponses = RedocNormalizedOptions.normalizeExpandResponses(raw.expandResponses);
Expand Down
11 changes: 10 additions & 1 deletion src/styled-components.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentClass, StatelessComponent } from 'react';
import * as styledComponents from 'styled-components';

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

export { ResolvedThemeInterface };

Expand Down Expand Up @@ -56,3 +56,12 @@ export const media = {
export { css, injectGlobal, keyframes, ThemeProvider, withProps };
export { StyledComponentClass } from 'styled-components';
export default styled;

export function extensionsHook(styledName: string) {
return props => {
if (!props.theme.extensionsHook) {
return;
}
return props.theme.extensionsHook(styledName, props);
};
}
5 changes: 5 additions & 0 deletions src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { adjustHue, desaturate, lighten, transparentize } from 'polished';

const defaultTheme: ThemeInterface = {
spacingUnit: 20,

breakpoints: {
small: '50rem',
medium: '85rem',
Expand Down Expand Up @@ -74,6 +75,7 @@ const defaultTheme: ThemeInterface = {
rightPanel: {
backgroundColor: '#263238',
width: '40%',
textColor: '#ffffff',
},
};

Expand Down Expand Up @@ -187,7 +189,10 @@ export interface ResolvedThemeInterface {
rightPanel: {
backgroundColor: string;
width: string;
textColor: string;
};

extensionsHook?: (name: string, props: any) => string;
}

export type primitive = string | number | boolean | undefined | null;
Expand Down

0 comments on commit 55bd853

Please sign in to comment.