@redocly/theme 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -0
- package/lib/package.json +91 -0
- package/lib/src/Button/Button.tsx +122 -0
- package/lib/src/Button/index.ts +1 -0
- package/lib/src/CodeBlock/CodeBlock.ts +125 -0
- package/lib/src/CodeBlock/index.ts +1 -0
- package/lib/src/CopyButton/CopyButton.tsx +26 -0
- package/lib/src/CopyButton/CopyButtonWrapper.tsx +52 -0
- package/lib/src/CopyButton/index.ts +2 -0
- package/lib/src/Headings/Headings.ts +23 -0
- package/lib/src/Headings/index.ts +1 -0
- package/lib/src/JsonViewer/JsonViewer.tsx +130 -0
- package/lib/src/JsonViewer/index.ts +1 -0
- package/lib/src/JsonViewer/styled.ts +103 -0
- package/lib/src/Logo/Logo.tsx +23 -0
- package/lib/src/Navbar/Navbar.tsx +60 -0
- package/lib/src/Navbar/NavbarItem.tsx +90 -0
- package/lib/src/Navbar/NavbarMenu.tsx +29 -0
- package/lib/src/Panel/CodePanel.ts +31 -0
- package/lib/src/Panel/ContentPanel.ts +43 -0
- package/lib/src/Panel/DarkHeader.ts +8 -0
- package/lib/src/Panel/Panel.ts +18 -0
- package/lib/src/Panel/PanelBody.ts +30 -0
- package/lib/src/Panel/PanelComponent.tsx +73 -0
- package/lib/src/Panel/PanelHeader.ts +25 -0
- package/lib/src/Panel/PanelHeaderTitle.ts +11 -0
- package/lib/src/Panel/index.ts +7 -0
- package/lib/src/SamplesPanelControls/SamplesPanelControls.ts +70 -0
- package/lib/src/SamplesPanelControls/index.ts +1 -0
- package/lib/src/SidebarLogo/SidebarLogo.tsx +47 -0
- package/lib/src/SidebarLogo/index.ts +1 -0
- package/lib/src/SourceCode/SourceCode.tsx +67 -0
- package/lib/src/SourceCode/index.ts +1 -0
- package/lib/src/Tooltip/Tooltip.tsx +171 -0
- package/lib/src/Tooltip/index.ts +1 -0
- package/lib/src/globalStyle.ts +512 -0
- package/lib/src/hooks/index.ts +3 -0
- package/lib/src/hooks/useControl.ts +20 -0
- package/lib/src/hooks/useMount.ts +8 -0
- package/lib/src/hooks/useUnmount.ts +10 -0
- package/lib/src/icons/ShelfIcon/ShelfIcon.tsx +45 -0
- package/lib/src/icons/ShelfIcon/index.ts +2 -0
- package/lib/src/icons/index.ts +1 -0
- package/lib/src/index.ts +14 -0
- package/lib/src/mocks/Link.tsx +7 -0
- package/lib/src/mocks/utils.ts +3 -0
- package/lib/src/utils/ClipboardService.ts +92 -0
- package/lib/src/utils/css-variables.ts +2 -0
- package/lib/src/utils/highlight.ts +81 -0
- package/lib/src/utils/index.ts +6 -0
- package/lib/src/utils/jsonToHtml.ts +122 -0
- package/lib/src/utils/media-css.ts +16 -0
- package/lib/src/utils/theme-helpers.ts +34 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(c) Copyright 2022 Redocly Inc, all rights reserved.
|
package/lib/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@redocly/theme",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Shared UI components",
|
|
5
|
+
"author": "team@redocly.com",
|
|
6
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"typings": "index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"lib"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"start": "npm run storybook",
|
|
14
|
+
"storybook": "start-storybook -p 6006",
|
|
15
|
+
"build-storybook": "build-storybook",
|
|
16
|
+
"clean": "rm -rf lib",
|
|
17
|
+
"compile": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
18
|
+
"build:copy": " cp -R src package.json .npmignore lib/",
|
|
19
|
+
"build": "npm run clean && npm run compile && npm run build:copy",
|
|
20
|
+
"build:watch": "npm run build && tsc --watch -p tsconfig.build.json & tsc-alias -w -p tsconfig.build.json",
|
|
21
|
+
"deploy": "npm publish lib",
|
|
22
|
+
"lint": "eslint . --ext .ts,.tsx --cache",
|
|
23
|
+
"lint:fix": "eslint . --ext .ts,.tsx --fix",
|
|
24
|
+
"prettier-base": "prettier \"**/*.{json,js,jsx,ts,tsx,yml,yaml,html,md}\"",
|
|
25
|
+
"prettier": "npm run prettier-base -- --write",
|
|
26
|
+
"prettier:check": "npm run prettier-base -- --check",
|
|
27
|
+
"ts:check": "tsc --noEmit --skipLibCheck",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch",
|
|
30
|
+
"test:coverage": "jest --coverage",
|
|
31
|
+
"prepare": "husky install",
|
|
32
|
+
"chromatic": "chromatic --exit-zero-on-changes"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"prismjs": "^1.28.0",
|
|
36
|
+
"react": "^17.0.2",
|
|
37
|
+
"react-dom": "^17.0.2",
|
|
38
|
+
"styled-components": "^5.3.1"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@storybook/addon-actions": "^6.4.22",
|
|
42
|
+
"@storybook/addon-essentials": "^6.4.22",
|
|
43
|
+
"@storybook/addon-interactions": "^6.4.22",
|
|
44
|
+
"@storybook/addon-links": "^6.4.22",
|
|
45
|
+
"@storybook/builder-webpack5": "^6.4.22",
|
|
46
|
+
"@storybook/core-common": "^6.4.22",
|
|
47
|
+
"@storybook/manager-webpack5": "^6.4.22",
|
|
48
|
+
"@storybook/node-logger": "^6.4.22",
|
|
49
|
+
"@storybook/preset-create-react-app": "^4.1.0",
|
|
50
|
+
"@storybook/react": "^6.4.22",
|
|
51
|
+
"@storybook/testing-library": "^0.0.11",
|
|
52
|
+
"@testing-library/jest-dom": "^5.16.4",
|
|
53
|
+
"@testing-library/react": "^12.1.4",
|
|
54
|
+
"@testing-library/user-event": "^13.5.0",
|
|
55
|
+
"@types/jest": "^27.4.1",
|
|
56
|
+
"@types/node": "^16.11.26",
|
|
57
|
+
"@types/prismjs": "^1.26.0",
|
|
58
|
+
"@types/react": "^17.0.43",
|
|
59
|
+
"@types/react-dom": "^17.0.14",
|
|
60
|
+
"@types/styled-components": "^5.1.25",
|
|
61
|
+
"@typescript-eslint/eslint-plugin": "^5.21.0",
|
|
62
|
+
"@typescript-eslint/parser": "^5.22.0",
|
|
63
|
+
"chromatic": "^6.5.4",
|
|
64
|
+
"esbuild": "^0.14.38",
|
|
65
|
+
"eslint": "^8.13.0",
|
|
66
|
+
"eslint-config-prettier": "^8.5.0",
|
|
67
|
+
"eslint-import-resolver-typescript": "^2.7.1",
|
|
68
|
+
"eslint-plugin-import": "^2.26.0",
|
|
69
|
+
"eslint-plugin-jest": "^26.1.4",
|
|
70
|
+
"eslint-plugin-react": "^7.29.4",
|
|
71
|
+
"eslint-plugin-react-hooks": "^4.5.0",
|
|
72
|
+
"eslint-plugin-storybook": "^0.5.11",
|
|
73
|
+
"husky": "^7.0.4",
|
|
74
|
+
"jest": "^27.5.1",
|
|
75
|
+
"jest-styled-components": "^7.0.8",
|
|
76
|
+
"lint-staged": "^12.4.1",
|
|
77
|
+
"prettier": "2.6.2",
|
|
78
|
+
"react": "^17.0.2",
|
|
79
|
+
"react-dom": "^17.0.2",
|
|
80
|
+
"react-refresh": "^0.13.0",
|
|
81
|
+
"react-scripts": "5.0.1",
|
|
82
|
+
"styled-components": "^5.3.5",
|
|
83
|
+
"ts-jest": "^27.1.4",
|
|
84
|
+
"tsc-alias": "^1.6.7",
|
|
85
|
+
"typescript": "^4.6.3",
|
|
86
|
+
"webpack": "^5.72.0"
|
|
87
|
+
},
|
|
88
|
+
"lint-staged": {
|
|
89
|
+
"**/*": "prettier --write --ignore-unknown"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import styled, { css, FlattenSimpleInterpolation, keyframes, Keyframes } from 'styled-components';
|
|
2
|
+
|
|
3
|
+
export type ButtonSize = 'small' | 'medium' | 'large' | 'xlarge' | string;
|
|
4
|
+
export type ButtonColor = 'primary' | 'secondary' | 'default' | string;
|
|
5
|
+
export type ButtonVariant = 'outlined' | 'contained';
|
|
6
|
+
|
|
7
|
+
export interface ButtonProps {
|
|
8
|
+
color?: ButtonColor;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
blinking?: boolean;
|
|
11
|
+
fullWidth?: boolean;
|
|
12
|
+
variant?: ButtonVariant;
|
|
13
|
+
size?: ButtonSize;
|
|
14
|
+
extraClass?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const getBlink = (): Keyframes => keyframes`
|
|
18
|
+
0% {
|
|
19
|
+
color: var(--button-color);
|
|
20
|
+
background-color: var(--button-active-background-color);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
50% {
|
|
24
|
+
background-color: var(--button-background-color);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
100% {
|
|
28
|
+
color: var(--button-color);
|
|
29
|
+
background-color: var(--button-active-background-color);
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
const getSize = (size: ButtonSize = 'medium'): FlattenSimpleInterpolation => css`
|
|
34
|
+
font-size: var(--button-${size}-font-size);
|
|
35
|
+
padding: var(--button-${size}-padding);
|
|
36
|
+
min-width: var(--button-${size}-min-width);
|
|
37
|
+
`;
|
|
38
|
+
|
|
39
|
+
export const baseButtonStyles = css`
|
|
40
|
+
outline: none;
|
|
41
|
+
border: none;
|
|
42
|
+
border-radius: var(--button-border-radius);
|
|
43
|
+
font-weight: var(--button-font-weight);
|
|
44
|
+
font-size: 14px;
|
|
45
|
+
padding: 2px 20px;
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
transition: background-color 0.25s ease, border-color 0.25s ease, box-shadow 0.25s ease;
|
|
48
|
+
line-height: 1;
|
|
49
|
+
|
|
50
|
+
font-family: var(--button-font-family);
|
|
51
|
+
box-shadow: var(--button-box-shadow);
|
|
52
|
+
|
|
53
|
+
&:hover {
|
|
54
|
+
box-shadow: var(--button-active-box-shadow);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
&:active {
|
|
58
|
+
box-shadow: var(--button-active-box-shadow);
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
export const Button = styled.button.attrs(({ color = 'default', extraClass }: ButtonProps) => ({
|
|
63
|
+
className: `button-color-${color}${extraClass ? ` ${extraClass}` : ''}`,
|
|
64
|
+
}))<ButtonProps>`
|
|
65
|
+
text-decoration: none;
|
|
66
|
+
text-align: center;
|
|
67
|
+
|
|
68
|
+
width: ${({ fullWidth }) => (fullWidth ? '100%' : 'auto')};
|
|
69
|
+
|
|
70
|
+
${baseButtonStyles}
|
|
71
|
+
|
|
72
|
+
${({ variant }) =>
|
|
73
|
+
variant === 'outlined'
|
|
74
|
+
? css`
|
|
75
|
+
color: var(--button-background-color);
|
|
76
|
+
border: 2px solid var(--button-background-color);
|
|
77
|
+
background-color: transparent;
|
|
78
|
+
&:hover {
|
|
79
|
+
border: 2px solid var(--button-active-background-color);
|
|
80
|
+
background-color: transparent;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
&:active {
|
|
84
|
+
border: 2px solid var(--button-outlined-active-border-color);
|
|
85
|
+
}
|
|
86
|
+
`
|
|
87
|
+
: css`
|
|
88
|
+
color: var(--button-color);
|
|
89
|
+
background-color: var(--button-background-color);
|
|
90
|
+
&:hover {
|
|
91
|
+
background-color: var(--button-hover-background-color);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
&:active {
|
|
95
|
+
background-color: var(--button-active-background-color);
|
|
96
|
+
}
|
|
97
|
+
`};
|
|
98
|
+
|
|
99
|
+
${({ size }) => getSize(size)}
|
|
100
|
+
|
|
101
|
+
& + & {
|
|
102
|
+
margin-left: 0.85em;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
${({ blinking }) =>
|
|
106
|
+
blinking &&
|
|
107
|
+
css`
|
|
108
|
+
pointer-events: none;
|
|
109
|
+
animation: ${getBlink()} 1.2s infinite;
|
|
110
|
+
`}
|
|
111
|
+
|
|
112
|
+
${({ disabled }) =>
|
|
113
|
+
disabled &&
|
|
114
|
+
css`
|
|
115
|
+
&,
|
|
116
|
+
&:hover {
|
|
117
|
+
cursor: default;
|
|
118
|
+
color: #999999;
|
|
119
|
+
pointer-events: none;
|
|
120
|
+
}
|
|
121
|
+
`}
|
|
122
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Button';
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import styled from 'styled-components';
|
|
2
|
+
|
|
3
|
+
export const CodeBlock = styled.div`
|
|
4
|
+
max-height: var(--code-block-max-height, 600px);
|
|
5
|
+
word-break: var(--code-block-word-break, initial);
|
|
6
|
+
/**
|
|
7
|
+
* Based on prism-dark.css
|
|
8
|
+
*/
|
|
9
|
+
code[class*='language-'],
|
|
10
|
+
pre[class*='language-'] {
|
|
11
|
+
/* color: white;
|
|
12
|
+
background: none; */
|
|
13
|
+
text-shadow: 0 -0.1em 0.2em black;
|
|
14
|
+
text-align: left;
|
|
15
|
+
white-space: pre;
|
|
16
|
+
word-spacing: normal;
|
|
17
|
+
word-break: normal;
|
|
18
|
+
word-wrap: normal;
|
|
19
|
+
line-height: 1.5;
|
|
20
|
+
|
|
21
|
+
-moz-tab-size: 4;
|
|
22
|
+
-o-tab-size: 4;
|
|
23
|
+
tab-size: 4;
|
|
24
|
+
|
|
25
|
+
-webkit-hyphens: none;
|
|
26
|
+
-moz-hyphens: none;
|
|
27
|
+
-ms-hyphens: none;
|
|
28
|
+
hyphens: none;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@media print {
|
|
32
|
+
code[class*='language-'],
|
|
33
|
+
pre[class*='language-'] {
|
|
34
|
+
text-shadow: none;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/* Code blocks */
|
|
39
|
+
pre[class*='language-'] {
|
|
40
|
+
padding: 1em;
|
|
41
|
+
margin: 0.5em 0;
|
|
42
|
+
overflow: auto;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.token.comment,
|
|
46
|
+
.token.prolog,
|
|
47
|
+
.token.doctype,
|
|
48
|
+
.token.cdata {
|
|
49
|
+
color: hsl(30, 20%, 50%);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.token.punctuation {
|
|
53
|
+
opacity: 1;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.namespace {
|
|
57
|
+
opacity: 0.7;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.token.property,
|
|
61
|
+
.token.tag,
|
|
62
|
+
.token.number,
|
|
63
|
+
.token.constant,
|
|
64
|
+
.token.symbol {
|
|
65
|
+
color: var(--color-content-inverse);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.token.boolean {
|
|
69
|
+
color: var(--color-error-500);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.token.selector,
|
|
73
|
+
.token.attr-name,
|
|
74
|
+
.token.string,
|
|
75
|
+
.token.char,
|
|
76
|
+
.token.builtin,
|
|
77
|
+
.token.inserted {
|
|
78
|
+
color: #fee39e;
|
|
79
|
+
|
|
80
|
+
& + a,
|
|
81
|
+
& + a:visited {
|
|
82
|
+
color: #4ed2ba;
|
|
83
|
+
text-decoration: underline;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.token.property.string {
|
|
88
|
+
color: #9efaa7;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.token.operator,
|
|
92
|
+
.token.entity,
|
|
93
|
+
.token.url,
|
|
94
|
+
.token.variable {
|
|
95
|
+
color: #f5b83d;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.token.atrule,
|
|
99
|
+
.token.attr-value,
|
|
100
|
+
.token.keyword {
|
|
101
|
+
color: #ffdbf4;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.token.regex,
|
|
105
|
+
.token.important {
|
|
106
|
+
color: #e90;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.token.important,
|
|
110
|
+
.token.bold {
|
|
111
|
+
font-weight: bold;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.token.italic {
|
|
115
|
+
font-style: italic;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.token.entity {
|
|
119
|
+
cursor: help;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.token.deleted {
|
|
123
|
+
color: red;
|
|
124
|
+
}
|
|
125
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './CodeBlock';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Button } from '../Button';
|
|
4
|
+
|
|
5
|
+
export interface CopyButtonProps {
|
|
6
|
+
text: string;
|
|
7
|
+
dataTestId?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const CopyButton = ({ text, dataTestId = 'copy-button' }: CopyButtonProps): JSX.Element => {
|
|
11
|
+
const [title, setTitle] = useState('Copy');
|
|
12
|
+
|
|
13
|
+
async function write() {
|
|
14
|
+
await navigator.clipboard.writeText(text);
|
|
15
|
+
|
|
16
|
+
setTitle('Copied!');
|
|
17
|
+
|
|
18
|
+
setTimeout(() => setTitle('Copy'), 1500);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<Button color="secondary" onClick={write} data-cy={dataTestId}>
|
|
23
|
+
{title}
|
|
24
|
+
</Button>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React, { memo } from 'react';
|
|
2
|
+
|
|
3
|
+
import { SamplesControlButton } from '../SamplesPanelControls';
|
|
4
|
+
import { Tooltip } from '../Tooltip';
|
|
5
|
+
import { useControl } from '../hooks';
|
|
6
|
+
import { ClipboardService } from '../utils';
|
|
7
|
+
|
|
8
|
+
export interface CopyButtonWrapperProps {
|
|
9
|
+
data: unknown;
|
|
10
|
+
children: (props: { renderCopyButton: () => JSX.Element }) => JSX.Element;
|
|
11
|
+
dataTestId?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function CopyButtonWrapperComponent({
|
|
15
|
+
data,
|
|
16
|
+
children,
|
|
17
|
+
dataTestId = 'copy-button',
|
|
18
|
+
}: CopyButtonWrapperProps): JSX.Element {
|
|
19
|
+
const tooltip = useControl();
|
|
20
|
+
|
|
21
|
+
const showTooltip = (): void => {
|
|
22
|
+
tooltip.handleOpen();
|
|
23
|
+
|
|
24
|
+
setTimeout(() => {
|
|
25
|
+
tooltip.handleClose();
|
|
26
|
+
}, 1500);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const copy = (): void => {
|
|
30
|
+
const content = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
|
|
31
|
+
ClipboardService.copyCustom(content);
|
|
32
|
+
showTooltip();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const renderCopyButton = (): JSX.Element => {
|
|
36
|
+
return (
|
|
37
|
+
<Tooltip
|
|
38
|
+
className="copy-button"
|
|
39
|
+
tip={ClipboardService.isSupported() ? 'Copied' : 'Not supported in your browser'}
|
|
40
|
+
open={tooltip.isOpened}
|
|
41
|
+
>
|
|
42
|
+
<SamplesControlButton onClick={copy} data-cy={dataTestId}>
|
|
43
|
+
Copy
|
|
44
|
+
</SamplesControlButton>
|
|
45
|
+
</Tooltip>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return children({ renderCopyButton });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const CopyButtonWrapper = memo<CopyButtonWrapperProps>(CopyButtonWrapperComponent);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import styled from 'styled-components';
|
|
2
|
+
|
|
3
|
+
import { typography } from '../utils';
|
|
4
|
+
|
|
5
|
+
export const H1 = styled.h1`
|
|
6
|
+
color: var(--color-content);
|
|
7
|
+
margin: 0 0 25px;
|
|
8
|
+
|
|
9
|
+
${typography('h1', 'h')};
|
|
10
|
+
`;
|
|
11
|
+
|
|
12
|
+
export const H2 = styled.h2`
|
|
13
|
+
color: var(--color-content);
|
|
14
|
+
margin: 0 0 20px;
|
|
15
|
+
|
|
16
|
+
${typography('h2', 'h')};
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
export const H3 = styled.h3`
|
|
20
|
+
color: var(--color-content);
|
|
21
|
+
|
|
22
|
+
${typography('h3', 'h')};
|
|
23
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Headings';
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import React, { memo, useRef, ReactElement } from 'react';
|
|
2
|
+
import styled from 'styled-components';
|
|
3
|
+
|
|
4
|
+
import { CopyButtonWrapper } from '../CopyButton';
|
|
5
|
+
import { SampleControls, SamplesControlButton } from '../SamplesPanelControls';
|
|
6
|
+
import { jsonToHTML } from '../utils';
|
|
7
|
+
import { useMount, useUnmount } from '../hooks';
|
|
8
|
+
import { CodeBlock } from '../CodeBlock';
|
|
9
|
+
|
|
10
|
+
import { jsonStyles } from './styled';
|
|
11
|
+
|
|
12
|
+
export interface JsonProps {
|
|
13
|
+
data: any;
|
|
14
|
+
className?: string;
|
|
15
|
+
jsonSampleExpandLevel: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function JsonComponent({ data, jsonSampleExpandLevel, className }: JsonProps): JSX.Element {
|
|
19
|
+
const node = useRef<HTMLDivElement | null>(null);
|
|
20
|
+
|
|
21
|
+
useMount(() => {
|
|
22
|
+
node?.current?.addEventListener('click', clickListener);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
useUnmount(() => {
|
|
26
|
+
node?.current?.removeEventListener('click', clickListener);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const expandAll = () => {
|
|
30
|
+
const elements = node?.current?.getElementsByClassName('collapsible');
|
|
31
|
+
for (const collapsed of Array.prototype.slice.call(elements)) {
|
|
32
|
+
const parentNode = collapsed.parentNode as Element;
|
|
33
|
+
if (!parentNode) continue;
|
|
34
|
+
parentNode.classList.remove('collapsed');
|
|
35
|
+
parentNode.querySelector('.collapser')?.setAttribute('aria-label', 'collapse');
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const collapseAll = () => {
|
|
40
|
+
const elements = node?.current?.getElementsByClassName('collapsible');
|
|
41
|
+
// skip first item to avoid collapsing whole object/array
|
|
42
|
+
const elementsArr = Array.prototype.slice.call(elements, 1);
|
|
43
|
+
|
|
44
|
+
for (const expanded of elementsArr) {
|
|
45
|
+
const parentNode = expanded.parentNode as Element;
|
|
46
|
+
if (!parentNode) continue;
|
|
47
|
+
parentNode.classList.add('collapsed');
|
|
48
|
+
parentNode.querySelector('.collapser')?.setAttribute('aria-label', 'expand');
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const collapseElement = (target: HTMLElement) => {
|
|
53
|
+
if (target.parentElement && target.className === 'collapser') {
|
|
54
|
+
const collapsed = target.parentElement.getElementsByClassName('collapsible')[0];
|
|
55
|
+
if (collapsed?.parentElement?.classList.contains('collapsed')) {
|
|
56
|
+
collapsed.parentElement.classList.remove('collapsed');
|
|
57
|
+
target.setAttribute('aria-label', 'collapse');
|
|
58
|
+
} else {
|
|
59
|
+
collapsed?.parentElement?.classList.add('collapsed');
|
|
60
|
+
target.setAttribute('aria-label', 'expand');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const clickListener = (event: MouseEvent) => {
|
|
66
|
+
collapseElement(event.target as HTMLElement);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const renderInner = ({ renderCopyButton }: { renderCopyButton: () => ReactElement }) => {
|
|
70
|
+
const showFoldingButtons =
|
|
71
|
+
data && Object.values(data).some((value) => typeof value === 'object' && value !== null);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<JsonViewerWrap data-cy="json-viewer">
|
|
75
|
+
<SampleControls data-cy="buttons">
|
|
76
|
+
{renderCopyButton()}
|
|
77
|
+
{showFoldingButtons && (
|
|
78
|
+
<>
|
|
79
|
+
<SamplesControlButton onClick={expandAll} data-cy="expand-all">
|
|
80
|
+
Expand all
|
|
81
|
+
</SamplesControlButton>
|
|
82
|
+
<SamplesControlButton onClick={collapseAll} data-cy="collapse-all">
|
|
83
|
+
Collapse all
|
|
84
|
+
</SamplesControlButton>
|
|
85
|
+
</>
|
|
86
|
+
)}
|
|
87
|
+
</SampleControls>
|
|
88
|
+
<StyledCodeBlock
|
|
89
|
+
className={className}
|
|
90
|
+
ref={node}
|
|
91
|
+
dangerouslySetInnerHTML={{
|
|
92
|
+
__html: jsonToHTML(data, jsonSampleExpandLevel),
|
|
93
|
+
}}
|
|
94
|
+
/>
|
|
95
|
+
</JsonViewerWrap>
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<CopyButtonWrapper data-cy="copy-button" data={data}>
|
|
101
|
+
{renderInner}
|
|
102
|
+
</CopyButtonWrapper>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export const Json = memo<JsonProps>(JsonComponent);
|
|
107
|
+
|
|
108
|
+
export const JsonViewer = styled(Json)<JsonProps>`
|
|
109
|
+
${jsonStyles};
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
export const JsonViewerWrap = styled.div`
|
|
113
|
+
display: flex;
|
|
114
|
+
flex-direction: column;
|
|
115
|
+
flex: 1;
|
|
116
|
+
|
|
117
|
+
&:hover > ${SampleControls} {
|
|
118
|
+
opacity: 1;
|
|
119
|
+
}
|
|
120
|
+
`;
|
|
121
|
+
|
|
122
|
+
export const StyledCodeBlock = styled(CodeBlock)`
|
|
123
|
+
flex: 1;
|
|
124
|
+
|
|
125
|
+
code {
|
|
126
|
+
font-family: var(--code-font-family);
|
|
127
|
+
font-size: 14px;
|
|
128
|
+
line-height: 18px;
|
|
129
|
+
}
|
|
130
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './JsonViewer';
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { css } from 'styled-components';
|
|
2
|
+
|
|
3
|
+
export const jsonStyles = css`
|
|
4
|
+
.redoc-json code > .collapser {
|
|
5
|
+
display: none;
|
|
6
|
+
pointer-events: none;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
contain: content;
|
|
10
|
+
overflow-x: auto;
|
|
11
|
+
padding: 10px;
|
|
12
|
+
border-radius: var(--global-border-radius);
|
|
13
|
+
background-color: var(--samples-panel-controls-background-color);
|
|
14
|
+
color: var(--color-content-inverse);
|
|
15
|
+
font-size: var(--code-font-size);
|
|
16
|
+
font-family: var(--code-font-family);
|
|
17
|
+
white-space: var(--code-wrap, pre);
|
|
18
|
+
|
|
19
|
+
.callback-function {
|
|
20
|
+
color: gray;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.collapser:after {
|
|
24
|
+
content: '-';
|
|
25
|
+
cursor: pointer;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.collapsed > .collapser:after {
|
|
29
|
+
content: '+';
|
|
30
|
+
cursor: pointer;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.ellipsis:after {
|
|
34
|
+
content: ' … ';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.collapsible {
|
|
38
|
+
margin-left: 2ch;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.hoverable {
|
|
42
|
+
padding: 1px 2px;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.hovered {
|
|
46
|
+
background-color: rgba(235, 238, 249, 1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.collapser {
|
|
50
|
+
background-color: transparent;
|
|
51
|
+
border: 0;
|
|
52
|
+
color: #fff;
|
|
53
|
+
display: flex;
|
|
54
|
+
align-items: center;
|
|
55
|
+
justify-content: center;
|
|
56
|
+
width: 15px;
|
|
57
|
+
height: 15px;
|
|
58
|
+
position: absolute;
|
|
59
|
+
top: 4px;
|
|
60
|
+
left: -1.5em;
|
|
61
|
+
cursor: default;
|
|
62
|
+
user-select: none;
|
|
63
|
+
-webkit-user-select: none;
|
|
64
|
+
padding: 2px;
|
|
65
|
+
font-family: var(--code-font-family);
|
|
66
|
+
font-size: var(--code-font-size);
|
|
67
|
+
&:focus {
|
|
68
|
+
outline: #fff dotted 1px;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
ul {
|
|
73
|
+
list-style-type: none;
|
|
74
|
+
padding: 0;
|
|
75
|
+
margin: 0 0 0 26px;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
li {
|
|
79
|
+
position: relative;
|
|
80
|
+
display: block;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.hoverable {
|
|
84
|
+
display: inline-block;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.selected {
|
|
88
|
+
outline-width: 1px;
|
|
89
|
+
outline-style: dotted;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.collapsed > .collapsible {
|
|
93
|
+
display: none;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.ellipsis {
|
|
97
|
+
display: none;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.collapsed > .ellipsis {
|
|
101
|
+
display: inherit;
|
|
102
|
+
}
|
|
103
|
+
`;
|