Skip to content

Commit a7cdf49

Browse files
committed
new-feature allow custom multi-theme
1 parent 8024388 commit a7cdf49

File tree

6 files changed

+161
-81
lines changed

6 files changed

+161
-81
lines changed

docs/examples/example100.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
getRapiDoc().setAttribute('layout', newLayout );
8484
}
8585

86+
function changeRenderStyle() {
87+
let currRender = getRapiDoc().getAttribute('render-style');
88+
let newRender = currRender === "read" ? "view" : "read";
89+
getRapiDoc().setAttribute('render-style', newRender );
90+
}
91+
8692
function toggleAttr(attr){
8793
if (getRapiDoc().getAttribute(attr) === 'false'){
8894
getRapiDoc().setAttribute(attr,"true");
@@ -131,6 +137,7 @@
131137
<rapi-doc id="thedoc" spec-url="https://petstore.swagger.io/v2/swagger.json">
132138
<div style="display:flex; margin:10px; justify-content:center;flex-wrap: wrap;">
133139
<button class="btn" onclick="changeAttr('header-color')">Change Header Color</button>
140+
<button class="btn" onclick="changeRenderStyle()">Render Style</button>
134141
<button class="btn" onclick="changeAttr('heading-text')">Change Heading Text</button>
135142
<button class="btn" onclick="changeLogo()">Change Logo</button>
136143
<button class="btn" onclick="changeFont()">Change Font</button>

src/components/schema-tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class SchemaTree extends LitElement {
3636
padding: 8px 16px 16px 16px;
3737
}
3838
.tree .tr:hover{
39-
background-color:rgba(128,128,128, 0.07);
39+
background-color:var(--hover-color);
4040
}
4141
.collapsed-descr .tr {
4242
max-height:20px;

src/rapidoc.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import FontStyles from '@/styles/font-styles';
66
import InputStyles from '@/styles/input-styles';
77
import FlexStyles from '@/styles/flex-styles';
88
import TableStyles from '@/styles/table-styles';
9-
import setTheme from '@/utils/theme';
10-
import ColorUtils from '@/utils/color-utils';
9+
import SetTheme from '@/utils/theme';
1110
import ProcessSpec from '@/utils/spec-parser';
1211

1312
import '@/components/m-logo';
@@ -64,6 +63,8 @@ export default class RapiDoc extends LitElement {
6463

6564
// Main Colors and Font
6665
theme: { type: String },
66+
bgColor: { type: String, attribute: 'bg-color' },
67+
textColor: { type: String, attribute: 'text-color' },
6768
headerColor: { type: String, attribute: 'header-color' },
6869
primaryColor: { type: String, attribute: 'primary-color' },
6970
regularFont: { type: String, attribute: 'regular-font' },
@@ -103,30 +104,26 @@ export default class RapiDoc extends LitElement {
103104

104105
/* eslint-disable indent */
105106
render() {
107+
const newTheme = {
108+
bg1: this.bgColor,
109+
fg1: this.textColor,
110+
headerColor: this.headerColor,
111+
primaryColor: this.primaryColor,
112+
navBgColor: this.navBgColor,
113+
navTextColor: this.navTextColor,
114+
navHoverBgColor: this.navHoverBgColor,
115+
navHoverTextColor: this.navHoverTextColor,
116+
navAccentColor: this.navAccentColor,
117+
};
106118
return html`
107119
${FontStyles}
108120
${InputStyles}
109121
${FlexStyles}
110122
${TableStyles}
111-
${this.theme === 'dark' ? setTheme('dark', {
112-
bg1: '#36312C',
113-
fg1: '#EBD1B7',
114-
lightFg: '#7A7267',
115-
}) : setTheme('light')}
123+
124+
${this.theme === 'dark' ? SetTheme('dark', newTheme) : SetTheme('light', newTheme)}
116125
<style>
117126
:host {
118-
--primary-color:${this.primaryColor ? `${this.primaryColor}` : '#FF791A'};
119-
--dark-primary-color:${ColorUtils.color.brightness(this.primaryColor ? this.primaryColor : '#FF791A', -30)};
120-
--primary-text:${this.primaryColor ? `${ColorUtils.color.invert(this.primaryColor)}` : '#ffffff'};
121-
--header-bg:${this.headerColor ? `${this.headerColor}` : '#444'};
122-
--header-fg:${this.headerColor ? `${ColorUtils.color.invert(this.headerColor)}` : '#ccc'};
123-
124-
--nav-bg-color:${this.navBgColor ? `${this.navBgColor}` : 'var(--bg2)'};
125-
--nav-text-color:${this.navTextColor ? `${this.navTextColor}` : 'var(--fg3)'};
126-
--nav-hover-bg-color:${this.navHoverBgColor ? `${this.navHoverBgColor}` : 'var(--hover-color)'};
127-
--nav-hover-text-color:${this.navHoverTextColor ? `${this.navHoverTextColor}` : 'var(--fg3)'};
128-
--nav-accent-color:${this.navAccentColor ? `${this.navAccentColor}` : 'var(--primary-color)'};
129-
130127
--layout:${this.layout ? `${this.layout}` : 'row'};
131128
--font-mono:${this.monoFont ? `${this.monoFont}` : 'Monaco, "Andale Mono", "Roboto Mono", Consolas'};
132129
--font-regular:${this.regularFont ? `${this.regularFont}` : 'rapidoc, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif'};
@@ -190,7 +187,7 @@ export default class RapiDoc extends LitElement {
190187
}
191188
192189
.nav-scroll:hover::-webkit-scrollbar {
193-
width: 30px;
190+
width: 24px;
194191
}
195192
196193
.nav-bar-tag {
@@ -273,9 +270,9 @@ export default class RapiDoc extends LitElement {
273270
}
274271
275272
input.header-input{
276-
background:${this.headerColor ? ColorUtils.color.brightness(this.headerColor, -20) : ColorUtils.color.inputReverseBg};
273+
background:var(--header-color-darker);
277274
color:var(--header-fg);
278-
border:1px solid var(--dark-primary-color);
275+
border:1px solid var(--header-color-border);
279276
flex:1;
280277
padding-right:24px;
281278
border-radius:3px;
@@ -657,6 +654,15 @@ export default class RapiDoc extends LitElement {
657654
this.loadSpec(newVal);
658655
}
659656
}
657+
if (name === 'render-style') {
658+
if (newVal === 'read') {
659+
window.setTimeout(() => {
660+
this.observeExpandedContent();
661+
}, 100);
662+
} else {
663+
this.intersectionObserver.disconnect();
664+
}
665+
}
660666
super.attributeChangedCallback(name, oldVal, newVal);
661667
}
662668

@@ -707,6 +713,12 @@ export default class RapiDoc extends LitElement {
707713
this.matchPaths = '';
708714
}
709715

716+
// Public Method
717+
updateTheme(baseTheme, objTheme = {}) {
718+
SetTheme(baseTheme, objTheme);
719+
}
720+
721+
// Public Method
710722
async loadSpec(specUrl) {
711723
if (!specUrl) {
712724
return;
@@ -750,14 +762,15 @@ export default class RapiDoc extends LitElement {
750762
this.selectedServer = this.resolvedSpec.servers[0].url;
751763
}
752764
}
765+
766+
this.requestUpdate();
753767
// Put it at the end of event loop, to allow loading all the child elements (must for larger specs)
754768
this.intersectionObserver.disconnect();
755769
if (this.renderStyle === 'read') {
756770
window.setTimeout(() => {
757771
this.observeExpandedContent();
758772
}, 100);
759773
}
760-
this.requestUpdate();
761774
}
762775

763776
onIntersect(entries) {

src/styles/input-styles.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ input, select, button {
99
color:var(--fg);
1010
}
1111
/* Button */
12-
.m-btn{
12+
.m-btn {
1313
border-radius: var(--border-radius);
1414
font-weight: 600;
1515
display: inline-block;
@@ -25,16 +25,16 @@ input, select, button {
2525
user-select: none;
2626
cursor: pointer;
2727
}
28-
.m-btn.primary{
28+
.m-btn.primary {
2929
background-color:var(--primary-color);
30-
color:var(--primary-text);
30+
color:var(--primary-color-invert);
3131
}
32-
.m-btn.large{padding:8px 14px;}
33-
.m-btn.small{padding:5px 12px;}
34-
.m-btn.circle{border-radius:50%;}
35-
.m-btn:hover{
32+
.m-btn.large { padding:8px 14px;}
33+
.m-btn.small { padding:5px 12px;}
34+
.m-btn.circle { border-radius:50%;}
35+
.m-btn:hover {
3636
background-color: var(--primary-color);
37-
color:var(--primary-text);
37+
color:var(--primary-color-invert);
3838
}
3939
.m-btn:disabled{
4040
background-color: var(--bg3);

src/styles/schema-styles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default html`
4949
font-size:10px;
5050
font-weight:bold;
5151
background-color:var(--primary-color);
52-
color:var(--primary-text);
52+
color:var(--primary-color-invert);
5353
border-radius:2px;
5454
line-height:18px;
5555
padding:0px 5px;

0 commit comments

Comments
 (0)