Skip to content

Commit 5f236b2

Browse files
committed
docs: allow edit components on StackBlitz
1 parent c2dc38f commit 5f236b2

File tree

13 files changed

+327
-171
lines changed

13 files changed

+327
-171
lines changed

docs/assets/js/application.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
2+
// IT'S ALL JUST JUNK FOR OUR DOCS!
3+
// ++++++++++++++++++++++++++++++++++++++++++
4+
5+
/*!
6+
* JavaScript for Bootstrap's docs (https://getbootstrap.com/)
7+
* Copyright 2011-2024 The Bootstrap Authors
8+
* Licensed under the Creative Commons Attribution 3.0 Unported License.
9+
* For details, see https://creativecommons.org/licenses/by/3.0/.
10+
*/
11+
12+
/* eslint-disable import/no-unresolved */
13+
import sidebarScroll from 'js/partials/sidebar.js'
14+
import codeExamples from 'js/partials/code-examples.js'
15+
import snippets from 'js/partials/snippets.js'
16+
/* eslint-enable import/no-unresolved */
17+
18+
sidebarScroll()
19+
codeExamples()
20+
snippets()

docs/assets/js/code-examples.js renamed to docs/assets/js/partials/code-examples.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/* global coreui: false */
1313
import ClipboardJS from 'clipboard'
1414

15-
(() => {
15+
export default () => {
1616
// Insert copy to clipboard button before .highlight
1717
const btnTitle = 'Copy to clipboard'
1818
const btnEdit = 'Edit on StackBlitz'
@@ -88,5 +88,5 @@ import ClipboardJS from 'clipboard'
8888
tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
8989
}, { once: true })
9090
})
91-
})()
91+
}
9292

docs/assets/js/partials/sidebar.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
2+
// IT'S ALL JUST JUNK FOR OUR DOCS!
3+
// ++++++++++++++++++++++++++++++++++++++++++
4+
5+
/*
6+
* JavaScript for Bootstrap's docs (https://getbootstrap.com/)
7+
* Copyright 2011-2024 The Bootstrap Authors
8+
* Licensed under the Creative Commons Attribution 3.0 Unported License.
9+
* For details, see https://creativecommons.org/licenses/by/3.0/.
10+
*/
11+
12+
export default () => {
13+
// Scroll the active sidebar link into view
14+
const sidebar = document.querySelector('.sidebar')
15+
const sidenav = document.querySelector('.sidebar-nav')
16+
const sidenavActiveLink = document.querySelector('.nav-item .active')
17+
18+
if (!sidenav || !sidenavActiveLink) {
19+
return
20+
}
21+
22+
const sidenavHeight = sidenav.clientHeight
23+
const sidenavActiveLinkTop = sidenavActiveLink.offsetTop + (sidebar.clientHeight - sidenavHeight)
24+
const sidenavActiveLinkHeight = sidenavActiveLink.clientHeight
25+
const viewportTop = sidenavActiveLinkTop
26+
const viewportBottom = viewportTop - sidenavHeight + sidenavActiveLinkHeight
27+
28+
if (sidenav.scrollTop > viewportTop || sidenav.scrollTop < viewportBottom) {
29+
sidenav.scrollTop = viewportTop - (sidenavHeight / 2) + (sidenavActiveLinkHeight / 2)
30+
}
31+
}

docs/assets/js/partials/snippets.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// NOTICE!!! Initially embedded in our docs this JavaScript
2+
// file contains elements that can help you create reproducible
3+
// use cases in StackBlitz for instance.
4+
// In a real project please adapt this content to your needs.
5+
// ++++++++++++++++++++++++++++++++++++++++++
6+
7+
/*!
8+
* JavaScript for CoreUI's docs (https://coreui.io/)
9+
* Copyright 2024 creativeLabs Lukasz Holeczek
10+
* Licensed under the Creative Commons Attribution 3.0 Unported License.
11+
* For details, see https://creativecommons.org/licenses/by/3.0/.
12+
*/
13+
14+
/* global coreui: false */
15+
16+
export default () => {
17+
// --------
18+
// Tooltips
19+
// --------
20+
// Instantiate all tooltips in a docs or StackBlitz
21+
document.querySelectorAll('[data-coreui-toggle="tooltip"]')
22+
.forEach(tooltip => {
23+
new coreui.Tooltip(tooltip)
24+
})
25+
26+
// --------
27+
// Popovers
28+
// --------
29+
// Instantiate all popovers in docs or StackBlitz
30+
document.querySelectorAll('[data-coreui-toggle="popover"]')
31+
.forEach(popover => {
32+
new coreui.Popover(popover)
33+
})
34+
35+
// -------------------------------
36+
// Toasts
37+
// -------------------------------
38+
// Used by 'Placement' example in docs or StackBlitz
39+
const toastPlacement = document.getElementById('toastPlacement')
40+
if (toastPlacement) {
41+
document.getElementById('selectToastPlacement').addEventListener('change', function () {
42+
if (!toastPlacement.dataset.originalClass) {
43+
toastPlacement.dataset.originalClass = toastPlacement.className
44+
}
45+
46+
toastPlacement.className = `${toastPlacement.dataset.originalClass} ${this.value}`
47+
})
48+
}
49+
50+
// Instantiate all toasts in docs pages only
51+
document.querySelectorAll('.docs-example .toast')
52+
.forEach(toastNode => {
53+
const toast = new coreui.Toast(toastNode, {
54+
autohide: false
55+
})
56+
57+
toast.show()
58+
})
59+
60+
// Instantiate all toasts in docs pages only
61+
// js-docs-start live-toast
62+
const toastTrigger = document.getElementById('liveToastBtn')
63+
const toastLiveExample = document.getElementById('liveToast')
64+
65+
if (toastTrigger) {
66+
const toastCoreUI = coreui.Toast.getOrCreateInstance(toastLiveExample)
67+
toastTrigger.addEventListener('click', () => {
68+
toastCoreUI.show()
69+
})
70+
}
71+
// js-docs-end live-toast
72+
73+
// -------------------------------
74+
// Alerts
75+
// -------------------------------
76+
// Used in 'Show live alert' example in docs or StackBlitz
77+
78+
// js-docs-start live-alert
79+
const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
80+
const appendAlert = (message, type) => {
81+
const wrapper = document.createElement('div')
82+
wrapper.innerHTML = [
83+
`<div class="alert alert-${type} alert-dismissible" role="alert">`,
84+
` <div>${message}</div>`,
85+
' <button type="button" class="btn-close" data-coreui-dismiss="alert" aria-label="Close"></button>',
86+
'</div>'
87+
].join('')
88+
89+
alertPlaceholder.append(wrapper)
90+
}
91+
92+
const alertTrigger = document.getElementById('liveAlertBtn')
93+
if (alertTrigger) {
94+
alertTrigger.addEventListener('click', () => {
95+
appendAlert('Nice, you triggered this alert message!', 'success')
96+
})
97+
}
98+
// js-docs-end live-alert
99+
100+
// --------
101+
// Carousels
102+
// --------
103+
// Instantiate all non-autoplaying carousels in docs or StackBlitz
104+
document.querySelectorAll('.carousel:not([data-coreui-ride="carousel"])')
105+
.forEach(carousel => {
106+
coreui.Carousel.getOrCreateInstance(carousel)
107+
})
108+
109+
// -------------------------------
110+
// Checks & Radios
111+
// -------------------------------
112+
// Indeterminate checkbox example in docs and StackBlitz
113+
document.querySelectorAll('.docs-example-indeterminate [type="checkbox"]')
114+
.forEach(checkbox => {
115+
if (checkbox.id.includes('Indeterminate')) {
116+
checkbox.indeterminate = true
117+
}
118+
})
119+
120+
// -------------------------------
121+
// Links
122+
// -------------------------------
123+
// Disable empty links in docs examples only
124+
document.querySelectorAll('.docs-content [href="#"]')
125+
.forEach(link => {
126+
link.addEventListener('click', event => {
127+
event.preventDefault()
128+
})
129+
})
130+
131+
// -------------------------------
132+
// Modal
133+
// -------------------------------
134+
// Modal 'Varying modal content' example in docs and StackBlitz
135+
// js-docs-start varying-modal-content
136+
const exampleModal = document.getElementById('exampleModal')
137+
if (exampleModal) {
138+
exampleModal.addEventListener('show.coreui.modal', event => {
139+
// Button that triggered the modal
140+
const button = event.relatedTarget
141+
// Extract info from data-coreui-* attributes
142+
const recipient = button.getAttribute('data-coreui-whatever')
143+
// If necessary, you could initiate an Ajax request here
144+
// and then do the updating in a callback.
145+
146+
// Update the modal's content.
147+
const modalTitle = exampleModal.querySelector('.modal-title')
148+
const modalBodyInput = exampleModal.querySelector('.modal-body input')
149+
150+
modalTitle.textContent = `New message to ${recipient}`
151+
modalBodyInput.value = recipient
152+
})
153+
}
154+
// js-docs-end varying-modal-content
155+
156+
// -------------------------------
157+
// Offcanvas
158+
// -------------------------------
159+
// 'Offcanvas components' example in docs only
160+
const myOffcanvas = document.querySelectorAll('.docs-example-offcanvas .offcanvas')
161+
if (myOffcanvas) {
162+
myOffcanvas.forEach(offcanvas => {
163+
offcanvas.addEventListener('show.coreui.offcanvas', event => {
164+
event.preventDefault()
165+
}, false)
166+
})
167+
}
168+
}

0 commit comments

Comments
 (0)