-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCodeExampleTabs.vue
More file actions
219 lines (203 loc) · 6.05 KB
/
CodeExampleTabs.vue
File metadata and controls
219 lines (203 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<template>
<dt-tab-group
class="code-example-tab-group"
activation-mode="auto"
:size="100"
@change="selectedPanelId = $event.selected"
>
<template #tabs>
<dt-stack direction="row" justify="between" align="center" class="d-w100p">
<div>
<dt-tab
:id="vueTabId"
label="Vue code"
:panel-id="vuePanelId"
selected
>
Vue
</dt-tab>
<dt-tab
v-if="!!htmlCode"
:id="htmlTabId"
label="HTML code"
:panel-id="htmlPanelId"
>
HTML
</dt-tab>
</div>
<!-- aria label blank so no tooltip displays since it would be redundant to the "Copy code" text -->
<copy-button
:text="selectedPanelId === htmlPanelId ? trimmedHtmlCode : trimmedVueCode"
aria-label=""
>
Copy Code
</copy-button>
</dt-stack>
</template>
<dt-tab-panel
:id="vuePanelId"
:tab-id="vueTabId"
>
<div ref="vuePanelRef" v-dt-scrollbar class="language-html d-hmx164" data-ext="html">
<pre class="language-html" v-html="highlightedVue" />
</div>
</dt-tab-panel>
<dt-tab-panel
v-if="!!htmlCode"
:id="htmlPanelId"
:tab-id="htmlTabId"
>
<dt-banner
v-if="showHtmlWarning"
class="d-ps-static"
kind="warning"
hide-close
>
Raw HTML renders visuals only. You may need to add JS to replicate its functionality.
</dt-banner>
<div ref="htmlPanelRef" v-dt-scrollbar class="language-html d-hmx164" data-ext="html">
<pre class="language-html" v-html="highlightedHtml" />
</div>
</dt-tab-panel>
<div
v-if="shouldShowButton"
class="code-example-tab-group__more d-ps-absolute d-bn8 d-l50p"
aria-hidden="true"
>
<dt-button
class="code-example-tab-group__more-btn d-bgc-secondary d-bs-sm"
kind="muted"
importance="outlined"
:size="100"
@click="expandCodeBlocks"
>
Show all
<template #endIcon="{ iconSize }">
<dt-icon name="arrow-down" :size="iconSize" />
</template>
</dt-button>
</div>
</dt-tab-group>
</template>
<script setup>
import { computed, onMounted, ref, nextTick } from 'vue';
import Prism from 'prismjs';
import prettier from 'prettier/standalone';
import htmlParser from 'prettier/plugins/html.mjs';
import CopyButton from './CopyButton.vue';
import { getUniqueString } from '@workspaceRoot/common/utils/client.mjs';
import { useDocExpandable } from '../composables/useDocExpandable.js';
const props = defineProps({
/**
* The HTML code to be displayed in the HTML tab if the component reference is not provided or
* a function that retrieves the reference to the example component to get the HTML code.
*/
htmlCode: {
type: [String, Function],
default: null,
},
/**
* The Vue code to be displayed in the Vue tab.
*/
vueCode: {
type: String,
required: true,
},
/**
* Indicates whether to show a warning for HTML code.
*/
showHtmlWarning: {
type: Boolean,
default: true,
},
});
const formattedHTML = ref(null);
const trimmedHtmlCode = computed(() => {
if (formattedHTML.value) {
return formattedHTML.value;
}
return typeof props.htmlCode === 'string' ? props.htmlCode.replace(/^\n/gm, '') : '';
});
const highlightedHtml = computed(() => {
if (formattedHTML.value) {
return Prism.highlight(
formattedHTML.value,
Prism.languages.html,
'html',
);
}
return typeof props.htmlCode === 'string' ? Prism.highlight(props.htmlCode.trim(), Prism.languages.html, 'html') : '';
});
const trimmedVueCode = props.vueCode.replace(/^\n/gm, '');
const highlightedVue = Prism.highlight(props.vueCode.trim(), Prism.languages.html, 'html');
const vueTabId = getUniqueString();
const vuePanelId = getUniqueString();
const htmlTabId = getUniqueString();
const htmlPanelId = getUniqueString();
const selectedPanelId = ref(vuePanelId);
// Expandable functionality
const vuePanelRef = ref(null);
const htmlPanelRef = ref(null);
const { shouldShowButton, handleExpand, initExpandable } = useDocExpandable({
maxHeightClass: 'd-hmx164',
});
/**
* Expand all code blocks by removing max-height class.
*/
const expandCodeBlocks = () => {
const panels = [vuePanelRef.value, htmlPanelRef.value].filter(Boolean);
handleExpand(panels);
};
onMounted(async () => {
if (typeof props.htmlCode === 'function') {
const componentRef = props.htmlCode();
const el = componentRef.$el ?? componentRef;
const formatted = await formatHTML(el.outerHTML);
formattedHTML.value = formatted;
}
// Initialize expandable after content is rendered
await nextTick();
const activePanel = selectedPanelId.value === vuePanelId ? vuePanelRef.value : htmlPanelRef.value;
if (activePanel) {
initExpandable(activePanel);
}
});
/**
* Transforms a single-line HTML string to an indented multiline HTML string.
* Removes comments, id and data-qa attributes, and simplifies svg tags.
* Also, adds a new line before each svg, img, and span tag because prettier
* doesn't do it.
* @param elementHTML - The HTML code to be formatted.
* @returns The formatted HTML code.
*/
const formatHTML = async (elementHTML) => {
const normalizedHTML = elementHTML
.replace(/<!--.*?-->/g, '')
.replace(/id=".*?"/g, '')
.replace(/data-qa=".*?"/g, '')
.replace(/<svg.*?>.*?<\/svg>/g, '<svg>...</svg>')
.replace(/<svg/g, '\n<svg')
.replace(/<img/g, '\n<img')
.replace(/<span/g, '\n<span');
const prettyHTML = await prettier.format(normalizedHTML,
{
parser: 'html',
plugins: [htmlParser],
htmlWhitespaceSensitivity: 'ignore',
});
return prettyHTML;
};
</script>
<style scoped lang="less">
.code-example-tab-group {
margin-block-start: var(--dt-size-500);
position: relative;
.language-html {
margin-block-start: 0;
position: relative;
}
&__more {
transform: translateX(calc(var(--dt-size-50-percent) * -1));
}
}
</style>