chore: update styled components typings

This commit is contained in:
Roman Hotsiy 2018-11-27 11:18:41 +02:00
parent af3eee1f52
commit 27d195ef38
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
19 changed files with 1647 additions and 488 deletions

View File

@ -4,16 +4,10 @@
*/ */
import * as React from 'react'; import * as React from 'react';
import styled, { StyledFunction } from 'styled-components'; import styled from '../src/styled-components';
function withProps<T, U extends HTMLElement = HTMLElement>( const DropDownItem = styled.li<{ active?: boolean }>`
styledFunction: StyledFunction<React.HTMLProps<U>>, ${(props: any) => (props.active ? 'background-color: #eee' : '')};
): StyledFunction<T & React.HTMLProps<U>> {
return styledFunction;
}
const DropDownItem = withProps<{ active: boolean }>(styled.li)`
${props => ((props as any).active ? 'background-color: #eee' : '')};
padding: 13px 16px; padding: 13px 16px;
&:hover { &:hover {
background-color: #eee; background-color: #eee;

View File

@ -1,7 +1,6 @@
import Dropdown from 'react-dropdown'; import Dropdown from 'react-dropdown';
import { StyledComponentClass } from 'styled-components'; import styled from '../styled-components';
import styled, { withProps } from '../styled-components';
export interface DropdownOption { export interface DropdownOption {
label: string; label: string;
@ -14,7 +13,7 @@ export interface DropdownProps {
onChange: (val: DropdownOption) => void; onChange: (val: DropdownOption) => void;
} }
export const StyledDropdown = withProps<DropdownProps>(styled(Dropdown))` export const StyledDropdown = styled(Dropdown)`
min-width: 100px; min-width: 100px;
display: inline-block; display: inline-block;
position: relative; position: relative;
@ -24,7 +23,7 @@ export const StyledDropdown = withProps<DropdownProps>(styled(Dropdown))`
.Dropdown-control { .Dropdown-control {
font-family: ${props => props.theme.typography.headings.fontFamily}; font-family: ${props => props.theme.typography.headings.fontFamily};
position: relative; position: relative;
font-size: .929em; font-size: 0.929em;
width: 100%; width: 100%;
line-height: 1.5em; line-height: 1.5em;
vertical-align: middle; vertical-align: middle;
@ -84,14 +83,14 @@ export const StyledDropdown = withProps<DropdownProps>(styled(Dropdown))`
padding: 0.4em; padding: 0.4em;
&.is-selected { &.is-selected {
background-color: rgba(0, 0, 0, 0.05) background-color: rgba(0, 0, 0, 0.05);
} }
&:hover { &:hover {
background-color: rgba(38, 50, 56, 0.12) background-color: rgba(38, 50, 56, 0.12);
} }
} }
` as StyledComponentClass<any, DropdownProps>; `;
export const SimpleDropdown = styled(StyledDropdown)` export const SimpleDropdown = styled(StyledDropdown)`
margin-left: 10px; margin-left: 10px;
@ -105,7 +104,7 @@ export const SimpleDropdown = styled(StyledDropdown)`
background: transparent; background: transparent;
&:hover { &:hover {
color: ${props => props.theme.colors.main}; color: ${props => props.theme.colors.primary.main};
box-shadow: none; box-shadow: none;
} }
} }

View File

@ -1,6 +1,6 @@
// import { transparentize } from 'polished'; // import { transparentize } from 'polished';
import styled, { extensionsHook, withProps } from '../styled-components'; import styled, { extensionsHook } from '../styled-components';
import { deprecatedCss } from './mixins'; import { deprecatedCss } from './mixins';
export const PropertiesTableCaption = styled.caption` export const PropertiesTableCaption = styled.caption`
@ -10,7 +10,7 @@ export const PropertiesTableCaption = styled.caption`
color: ${props => props.theme.colors.text.secondary}; color: ${props => props.theme.colors.text.secondary};
`; `;
export const PropertyCell = styled.td` export const PropertyCell = styled.td<{ kind?: string }>`
border-left: 1px solid ${props => props.theme.schema.linesColor}; border-left: 1px solid ${props => props.theme.schema.linesColor};
box-sizing: border-box; box-sizing: border-box;
position: relative; position: relative;
@ -58,7 +58,7 @@ export const PropertyCellWithInner = styled(PropertyCell)`
padding: 0; padding: 0;
`; `;
export const PropertyNameCell = withProps<{ kind?: string }>(styled(PropertyCell))` export const PropertyNameCell = styled(PropertyCell)`
vertical-align: top; vertical-align: top;
line-height: 20px; line-height: 20px;
white-space: nowrap; white-space: nowrap;

View File

@ -1,5 +1,5 @@
import { SECTION_ATTR } from '../services/MenuStore'; import { SECTION_ATTR } from '../services/MenuStore';
import styled, { media, withProps } from '../styled-components'; import styled, { media, WithProps } from '../styled-components';
export const MiddlePanel = styled.div` export const MiddlePanel = styled.div`
width: calc(100% - ${props => props.theme.rightPanel.width}); width: calc(100% - ${props => props.theme.rightPanel.width});
@ -12,17 +12,15 @@ export const MiddlePanel = styled.div`
`}; `};
`; `;
export const Section = withProps<{ underlined?: boolean }>( export const Section = (styled.div as WithProps<'div', { underlined?: boolean }>).attrs(props => ({
styled.div.attrs({ [SECTION_ATTR]: props.id,
[SECTION_ATTR]: props => props.id, }))`
} as any),
)`
padding: ${props => props.theme.spacing.sectionVertical}px 0; padding: ${props => props.theme.spacing.sectionVertical}px 0;
${media.lessThan('medium', true)` ${media.lessThan('medium', true)`
padding: 0; padding: 0;
`} `}
${props => ${(props: any) =>
(props.underlined && (props.underlined &&
` `
position: relative; position: relative;

View File

@ -1,4 +1,4 @@
import styled, { withProps } from '../styled-components'; import styled from '../styled-components';
export const OneOfList = styled.ul` export const OneOfList = styled.ul`
margin: 0; margin: 0;
@ -15,7 +15,7 @@ export const OneOfLabel = styled.span`
} }
`; `;
export const OneOfButton = withProps<{ active: boolean }>(styled.li)` export const OneOfButton = styled.li<{ active: boolean }>`
display: inline-block; display: inline-block;
margin-right: 10px; margin-right: 10px;
font-size: 0.8em; font-size: 0.8em;

View File

@ -1,5 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import styled, { withProps } from '../styled-components'; import styled from '../styled-components';
const directionMap = { const directionMap = {
left: '90deg', left: '90deg',
@ -48,7 +48,7 @@ export const ShelfIcon = styled(IntShelfIcon)`
} }
`; `;
export const Badge = withProps<{ type: string }>(styled.span)` export const Badge = styled.span<{ type: string }>`
display: inline-block; display: inline-block;
padding: 0 5px; padding: 0 5px;
margin: 0; margin: 0;

View File

@ -24,12 +24,6 @@ export class ApiLogo extends React.Component<{ info: OpenAPIInfo }> {
alt={altText} alt={altText}
/> />
); );
return ( return <LogoWrap>{logoHref ? LinkWrap(logoHref)(logo) : logo}</LogoWrap>;
<LogoWrap>
{
logoHref ? LinkWrap(logoHref)(logo) : logo
}
</LogoWrap>
);
} }
} }

View File

@ -1,4 +1,4 @@
import styled, { withProps } from '../../styled-components'; import styled from '../../styled-components';
export const OperationEndpointWrap = styled.div` export const OperationEndpointWrap = styled.div`
cursor: pointer; cursor: pointer;
@ -14,7 +14,7 @@ export const ServerRelativeURL = styled.span`
text-overflow: ellipsis; text-overflow: ellipsis;
`; `;
export const EndpointInfo = withProps<{ expanded?: boolean; inverted?: boolean }>(styled.div)` export const EndpointInfo = styled.div<{ expanded?: boolean; inverted?: boolean }>`
padding: 10px 30px 10px ${props => (props.inverted ? '10px' : '20px')}; padding: 10px 30px 10px ${props => (props.inverted ? '10px' : '20px')};
border-radius: ${props => (props.inverted ? '0' : '4px 4px 0 0')}; border-radius: ${props => (props.inverted ? '0' : '4px 4px 0 0')};
background-color: ${props => (props.inverted ? 'transparent' : '#222d32')}; background-color: ${props => (props.inverted ? 'transparent' : '#222d32')};
@ -33,12 +33,12 @@ export const EndpointInfo = withProps<{ expanded?: boolean; inverted?: boolean }
} }
`; `;
export const HttpVerb = withProps<{ type: string }>(styled.span).attrs({ export const HttpVerb = styled.span.attrs((props: { type: string }) => ({
className: props => `http-verb ${props.type}`, className: `http-verb ${props.type}`,
})` }))<{ type: string }>`
font-size: 0.929em; font-size: 0.929em;
line-height: 20px; line-height: 20px;
background-color: ${props => props.theme.colors.http[props.type] || '#999999'}; background-color: ${(props: any) => props.theme.colors.http[props.type] || '#999999'};
color: #ffffff; color: #ffffff;
padding: 3px 10px; padding: 3px 10px;
text-transform: uppercase; text-transform: uppercase;
@ -46,7 +46,7 @@ export const HttpVerb = withProps<{ type: string }>(styled.span).attrs({
margin: 0; margin: 0;
`; `;
export const ServersOverlay = withProps<{ expanded: boolean }>(styled.div)` export const ServersOverlay = styled.div<{ expanded: boolean }>`
position: absolute; position: absolute;
width: 100%; width: 100%;
z-index: 100; z-index: 100;

View File

@ -1,10 +1,10 @@
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import * as React from 'react'; import * as React from 'react';
import styled, { withProps } from '../../styled-components'; import styled from '../../styled-components';
import { OpenAPIExternalDocumentation } from '../../types'; import { OpenAPIExternalDocumentation } from '../../types';
import { linksCss } from '../Markdown/styled.elements'; import { linksCss } from '../Markdown/styled.elements';
const LinkWrap = withProps<{ compact?: boolean }>(styled.div)` const LinkWrap = styled.div<{ compact?: boolean }>`
${linksCss}; ${linksCss};
${({ compact }) => (!compact ? 'margin: 1em 0' : '')} ${({ compact }) => (!compact ? 'margin: 1em 0' : '')}
`; `;

View File

@ -1,9 +1,9 @@
import * as React from 'react'; import * as React from 'react';
import styled, { withProps } from '../../styled-components'; import styled from '../../styled-components';
import { Spinner } from './Spinner.svg'; import { Spinner } from './Spinner.svg';
const LoadingMessage = withProps<{ color: string }>(styled.div)` const LoadingMessage = styled.div<{ color: string }>`
font-family: helvetica, sans; font-family: helvetica, sans;
width: 100%; width: 100%;
text-align: center; text-align: center;

View File

@ -1,6 +1,8 @@
import { headerCommonMixin, linkifyMixin } from '../../common-elements'; import { headerCommonMixin, linkifyMixin } from '../../common-elements';
import { PrismDiv } from '../../common-elements/PrismDiv'; import { PrismDiv } from '../../common-elements/PrismDiv';
import styled, { css, extensionsHook, withProps } from '../../styled-components'; import styled, { css, extensionsHook, ResolvedThemeInterface } from '../../styled-components';
import { StyledComponent } from 'styled-components';
export const linksCss = css` export const linksCss = css`
a { a {
@ -17,9 +19,11 @@ export const linksCss = css`
} }
`; `;
export const StyledMarkdownBlock = withProps<{ compact?: boolean; inline?: boolean }>( export const StyledMarkdownBlock = styled(PrismDiv as StyledComponent<
styled(PrismDiv), 'div',
)` ResolvedThemeInterface,
{ compact?: boolean; inline?: boolean }
>)`
font-family: ${props => props.theme.typography.fontFamily}; font-family: ${props => props.theme.typography.fontFamily};
font-weight: ${props => props.theme.typography.fontWeightRegular}; font-weight: ${props => props.theme.typography.fontWeightRegular};

View File

@ -32,7 +32,7 @@ export class MediaTypeSamples extends React.Component<PayloadSamplesProps> {
} }
if (examplesNames.length > 1) { if (examplesNames.length > 1) {
return ( return (
<SmallTabs> <SmallTabs defaultIndex={0}>
<TabList> <TabList>
{examplesNames.map(name => ( {examplesNames.map(name => (
<Tab key={name}> {examples[name].summary || name} </Tab> <Tab key={name}> {examples[name].summary || name} </Tab>

View File

@ -7,9 +7,9 @@ export const SearchWrap = styled.div`
padding: 5px 0; padding: 5px 0;
`; `;
export const SearchInput = styled.input.attrs({ export const SearchInput = styled.input.attrs(() => ({
className: 'search-input', className: 'search-input',
})` }))`
width: calc(100% - ${props => props.theme.spacing.unit * 8}px); width: calc(100% - ${props => props.theme.spacing.unit * 8}px);
box-sizing: border-box; box-sizing: border-box;
margin: 0 ${props => props.theme.spacing.unit * 4}px; margin: 0 ${props => props.theme.spacing.unit * 4}px;

View File

@ -1,11 +1,13 @@
import * as classnames from 'classnames'; import * as classnames from 'classnames';
import { deprecatedCss, ShelfIcon } from '../../common-elements'; import { deprecatedCss, ShelfIcon } from '../../common-elements';
import styled, { css, withProps } from '../../styled-components'; import styled, { css, WithProps } from '../../styled-components';
export const OperationBadge = withProps<{ type: string }>(styled.span).attrs({ export const OperationBadge = (styled.span as WithProps<'span', { type: string }>).attrs(
className: props => `operation-type ${props.type}`, (props: any) => ({
})` className: `operation-type ${props.type}`,
}),
)`
width: 32px; width: 32px;
display: inline-block; display: inline-block;
height: ${props => props.theme.typography.code.fontSize}; height: ${props => props.theme.typography.code.fontSize};
@ -71,7 +73,7 @@ function menuItemActiveBg(depth): string {
} }
} }
export const MenuItemUl = withProps<{ expanded: boolean }>(styled.ul)` export const MenuItemUl = styled.ul<{ expanded: boolean }>`
margin: 0; margin: 0;
padding: 0; padding: 0;
@ -82,7 +84,7 @@ export const MenuItemUl = withProps<{ expanded: boolean }>(styled.ul)`
${props => (props.expanded ? '' : 'display: none;')}; ${props => (props.expanded ? '' : 'display: none;')};
`; `;
export const MenuItemLi = withProps<{ depth: number }>(styled.li)` export const MenuItemLi = styled.li<{ depth: number }>`
list-style: none inside none; list-style: none inside none;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
@ -111,18 +113,19 @@ export const menuItemDepth = {
`, `,
}; };
export const MenuItemLabel = withProps<{ export interface MenuItemLabelType {
depth: number; depth: number;
active: boolean; active: boolean;
deprecated?: boolean; deprecated?: boolean;
type?: string; type?: string;
}>(styled.label).attrs({ }
export const MenuItemLabel = styled.label.attrs((props: MenuItemLabelType) => ({
role: 'menuitem', role: 'menuitem',
className: props => className: classnames('-depth' + props.depth, {
classnames('-depth' + props.depth, {
active: props.active, active: props.active,
}), }),
})` }))<MenuItemLabelType>`
cursor: pointer; cursor: pointer;
color: ${props => color: ${props =>
props.active ? props.theme.colors.primary.main : props.theme.colors.text.primary}; props.active ? props.theme.colors.primary.main : props.theme.colors.text.primary};
@ -151,7 +154,7 @@ export const MenuItemLabel = withProps<{
} }
`; `;
export const MenuItemTitle = withProps<{ width?: string }>(styled.span)` export const MenuItemTitle = styled.span<{ width?: string }>`
display: inline-block; display: inline-block;
vertical-align: middle; vertical-align: middle;
width: ${props => (props.width ? props.width : 'auto')}; width: ${props => (props.width ? props.width : 'auto')};

View File

@ -3,7 +3,7 @@ import * as React from 'react';
import { MenuStore } from '../../services/MenuStore'; import { MenuStore } from '../../services/MenuStore';
import { RedocNormalizedOptions, RedocRawOptions } from '../../services/RedocNormalizedOptions'; import { RedocNormalizedOptions, RedocRawOptions } from '../../services/RedocNormalizedOptions';
import styled, { media, withProps } from '../../styled-components'; import styled, { media } from '../../styled-components';
import { IS_BROWSER } from '../../utils/index'; import { IS_BROWSER } from '../../utils/index';
import { OptionsContext } from '../OptionsProvider'; import { OptionsContext } from '../OptionsProvider';
import { AnimatedChevronButton } from './ChevronSvg'; import { AnimatedChevronButton } from './ChevronSvg';
@ -21,7 +21,7 @@ export interface StickySidebarProps {
const stickyfill = Stickyfill && Stickyfill(); const stickyfill = Stickyfill && Stickyfill();
const StyledStickySidebar = withProps<{ open?: boolean }>(styled.div)` const StyledStickySidebar = styled.div<{ open?: boolean }>`
width: ${props => props.theme.menu.width}; width: ${props => props.theme.menu.width};
background-color: ${props => props.theme.menu.backgroundColor}; background-color: ${props => props.theme.menu.backgroundColor};
overflow: hidden; overflow: hidden;

View File

@ -4,7 +4,9 @@ import defaultTheme from '../theme';
export default class TestThemeProvider extends React.Component { export default class TestThemeProvider extends React.Component {
render() { render() {
return <ThemeProvider theme={defaultTheme}>{this.props.children}</ThemeProvider>; return (
<ThemeProvider theme={defaultTheme}>{React.Children.only(this.props.children)}</ThemeProvider>
);
} }
} }

View File

@ -5,15 +5,10 @@ import { ResolvedThemeInterface } from './theme';
export { ResolvedThemeInterface }; export { ResolvedThemeInterface };
export type InterpolationFunction<P> = styledComponents.InterpolationFunction<P>; type WithProps<
C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
export type StyledFunction<T> = styledComponents.ThemedStyledFunction<T, ResolvedThemeInterface>; T extends object
> = styledComponents.ThemedStyledFunction<C, ResolvedThemeInterface, T>;
function withProps<T, U extends HTMLElement = HTMLElement>(
styledFunction: StyledFunction<React.HTMLProps<U>>,
): StyledFunction<T & React.HTMLProps<U>> {
return styledFunction;
}
const { const {
default: styled, default: styled,
@ -54,7 +49,7 @@ export const media = {
}, },
}; };
export { css, createGlobalStyle, keyframes, ThemeProvider, withProps }; export { css, createGlobalStyle, keyframes, ThemeProvider, WithProps };
export default styled; export default styled;
export function extensionsHook(styledName: string) { export function extensionsHook(styledName: string) {

View File

@ -1,40 +1,15 @@
import * as styledComponents from 'styled-components'; import * as styledComponents from 'styled-components';
// Styled components typings for using babel-plugin BEFORE typescript // FIXME
declare module 'styled-components' { declare module 'styled-components' {
type Attrs<P, A extends Partial<P>, T> = { export interface ThemedStyledFunction<
[K in keyof A]: ((props: ThemedStyledProps<P, T>) => A[K]) | A[K] C extends keyof JSX.IntrinsicElements | React.ComponentType<any>,
}; T extends object,
type KeyofBase = keyof any; O extends object = {},
type Diff<T extends KeyofBase, U extends KeyofBase> = ({ [P in T]: P } & { [P in U]: never })[T]; A extends keyof any = never
type DiffBetween<T, U> = Pick<T, Diff<keyof T, keyof U>> & Pick<U, Diff<keyof U, keyof T>>; > extends ThemedStyledFunctionBase<C, T, O, A> {
interface ThemedStyledFunction<P, T, O = P> { withConfig(config: any): any;
// adding "| string[]" for transpileTemplateLiterals and similar below // tslint:enable:unified-signatures
(
strings: TemplateStringsArray | string[],
...interpolations: Array<Interpolation<ThemedStyledProps<P, T>>>
): StyledComponentClass<P, T, O>;
<U>(
strings: TemplateStringsArray | string[],
...interpolations: Array<Interpolation<ThemedStyledProps<P & U, T>>>
): StyledComponentClass<P & U, T, O & U>;
attrs<U, A extends Partial<P & U> = {}>(
attrs: Attrs<P & U, A, T>,
): ThemedStyledFunction<DiffBetween<A, P & U>, T, DiffBetween<A, O & U>>;
// adding "withConfig" for transpileTemplateLiterals
withConfig(config: any): ThemedStyledFunction<P, T, O>;
}
export interface BaseThemedCssFunction<T> {
(
strings: TemplateStringsArray | string[],
...interpolations: SimpleInterpolation[]
): InterpolationValue[];
<P>(
strings: TemplateStringsArray | string[],
...interpolations: Array<Interpolation<ThemedStyledProps<P, T>>>
): Array<FlattenInterpolation<ThemedStyledProps<P, T>>>;
} }
interface ThemedStyledComponentsModule<T> { interface ThemedStyledComponentsModule<T> {
@ -42,10 +17,5 @@ declare module 'styled-components' {
strings: TemplateStringsArray | string[], strings: TemplateStringsArray | string[],
...interpolations: SimpleInterpolation[] ...interpolations: SimpleInterpolation[]
): Keyframes; ): Keyframes;
createGlobalStyle<P = {}>(
strings: TemplateStringsArray | string[],
...interpolations: Array<Interpolation<ThemedStyledProps<P, T>>>
): GlobalStyleClass<P, T>;
} }
} }

1924
yarn.lock

File diff suppressed because it is too large Load Diff