Skip to content

Commit 4a23755

Browse files
committed
docs: update content
1 parent 954d4b2 commit 4a23755

File tree

2 files changed

+148
-85
lines changed

2 files changed

+148
-85
lines changed

docs/content/getting-started/javascript.md

Lines changed: 145 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Plugins can be included individually (using CoreUI's individual `js/dist/*.js`),
1212

1313
If you use a bundler (Webpack, Rollup...), you can use `/js/dist/*.js` files which are UMD ready.
1414

15+
## Usage with JavaScript frameworks
16+
17+
- Angular: [CoreUI for Angular](https://coreui.io/angular/docs/getting-started/introduction)
18+
- React: [CoreUI for React](https://coreui.io/react/docs/getting-started/introduction/)
19+
- Vue: [CoreUI for Vue](https://coreui.io/vue/docs/getting-started/introduction.html)
20+
1521
## Using CoreUI for Bootstrap as a module
1622

1723
We provide a version of CoreUI for Bootstrap built as `ESM` (`coreui.esm.js` and `coreui.esm.min.js`) which allows you to use CoreUI for Bootstrap as a module in your browser, if your [targeted browsers support it](https://caniuse.com/es6-module).
@@ -26,32 +32,67 @@ We provide a version of CoreUI for Bootstrap built as `ESM` (`coreui.esm.js` and
2632
</script>
2733
```
2834

29-
{{< callout warning >}}
30-
## Incompatible plugins
35+
Compared to JS bundlers, using ESM in the browser requires you to use the full path and filename instead of the module name. [Read more about JS modules in the browser.](https://v8.dev/features/modules#specifiers) That's why we use `'coreui.esm.min.js'` instead of `'coreui'` above. However, this is further complicated by our Popper dependency, which imports Popper into our JavaScript like so:
36+
37+
<!-- eslint-skip -->
38+
```js
39+
import * as Popper from "@popperjs/core"
40+
```
41+
42+
If you try this as-is, you'll see an error in the console like the following:
43+
44+
```text
45+
Uncaught TypeError: Failed to resolve module specifier "@popperjs/core". Relative references must start with either "/", "./", or "../".
46+
```
47+
48+
To fix this, you can use an `importmap` to resolve the arbitrary module names to complete paths. If your [targeted browsers](https://caniuse.com/?search=importmap) do not support `importmap`, you'll need to use the [es-module-shims](https://github.com/guybedford/es-module-shims) project. Here's how it works for Bootstrap and Popper:
3149

32-
Due to browser limitations, some of our plugins, namely Dropdown, Tooltip and Popover plugins, cannot be used in a `<script>` tag with `module` type because they depend on Popper. For more information about the issue see [here](https://v8.dev/features/modules#specifiers).
33-
{{< /callout >}}
50+
<!-- eslint-skip -->
51+
```html
52+
<!doctype html>
53+
<html lang="en">
54+
<head>
55+
<meta charset="utf-8">
56+
<meta name="viewport" content="width=device-width, initial-scale=1">
57+
<link href="{{< param "cdn.css" >}}" rel="stylesheet" integrity="{{< param "cdn.css_hash" >}}" crossorigin="anonymous">
58+
<title>Hello, modularity!</title>
59+
</head>
60+
<body>
61+
<h1>Hello, modularity!</h1>
62+
<button id="popoverButton" type="button" class="btn btn-primary btn-lg" data-coreui-toggle="popover" title="ESM in Browser" data-coreui-content="Bang!">Custom popover</button>
63+
64+
<script async src="https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.min.js" crossorigin="anonymous"></script>
65+
<script type="importmap">
66+
{
67+
"imports": {
68+
"@popperjs/core": "{{< param "cdn.popper_esm" >}}",
69+
"coreui": "https://cdn.jsdelivr.net/npm/coreui@{{< param "current_version" >}}/dist/js/coreui.esm.min.js"
70+
}
71+
}
72+
</script>
73+
<script type="module">
74+
import * as coreui from '@coreui/coreui'
75+
76+
new coreui.Popover(document.getElementById('popoverButton'))
77+
</script>
78+
</body>
79+
</html>
80+
```
3481

3582
## Dependencies
3683

3784
Some plugins and CSS components depend on other plugins. If you include plugins individually, make sure to check for these dependencies in the docs.
3885

3986
Our dropdowns, popovers and tooltips also depend on [Popper](https://popper.js.org/).
4087

41-
## Still want to use jQuery? It's possible!
42-
43-
CoreUI 4 for Bootstrap is designed to be used without jQuery, but it's still possible to use our components with jQuery. **If CoreUI for Bootstrap detects `jQuery` in the `window` object** it'll add all of our components in jQuery's plugin system; this means you'll be able to do `$('[data-coreui-toggle="tooltip"]').tooltip()` to enable tooltips. The same goes for our other components.
44-
4588
## Data attributes
4689

4790
Nearly all CoreUI for Bootstrap plugins can be enabled and configured through HTML alone with data attributes (our preferred way of using JavaScript functionality). Be sure to **only use one set of data attributes on a single element** (e.g., you cannot trigger a tooltip and modal from the same button.)
4891

49-
{{< callout warning >}}
5092
## Selectors
5193

5294
Currently to query DOM elements we use the native methods `querySelector` and `querySelectorAll` for performance reasons, so you have to use [valid selectors](https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier).
5395
If you use special selectors, for example: `collapse:Example` be sure to escape them.
54-
{{< /callout >}}
5596

5697
## Events
5798

@@ -70,38 +111,43 @@ myModal.addEventListener('show.coreui.modal', event => {
70111
})
71112
```
72113

73-
{{< callout warning >}}
74-
## jQuery events
114+
## Programmatic API
75115

76-
CoreUI for Bootstrap will detect jQuery if `jQuery` is present in the `window` object and there is no `data-coreui-no-jquery` attribute set on `<body>`. If jQuery is found, CoreUI for Bootstrap will emit events thanks to jQuery's event system. So if you want to listen to CoreUI's events, you'll have to use the jQuery methods (`.on`, `.one`) instead of `addEventListener`.
116+
All constructors accept an optional options object or nothing (which initiates a plugin with its default behavior):
77117

78118
```js
79-
$('#myTab a').on('shown.coreui.tab', () => {
80-
// do something...
81-
})
82-
```
83-
{{< /callout >}}
119+
const myModalEl = document.querySelector('#myModal')
120+
const modal = new coreui.Modal(myModalEl) // initialized with defaults
84121

85-
## Programmatic API
122+
const configObject = { keyboard: false }
123+
const modal1 = new coreui.Modal(myModalEl, configObject) // initialized with no keyboard
124+
```
86125

87-
All constructors accept an optional options object or nothing (which initiates a plugin with its default behavior):
126+
If you'd like to get a particular plugin instance, each plugin exposes a `getInstance` method. For example, to retrieve an instance directly from an element:
88127

89128
```js
90-
const myModalEl = document.getElementById('myModal')
129+
coreui.Popover.getInstance(myPopoverEl)
130+
```
91131

92-
const modal = new coreui.Modal(myModalEl) // initialized with defaults
93-
const modal1 = new coreui.Modal(myModalEl, { keyboard: false }) // initialized with no keyboard
132+
This method will return `null` if an instance is not initiated over the requested element.
133+
134+
Alternatively, `getOrCreateInstance` can be used to get the instance associated with a DOM element, or create a new one in case it wasn't initialized.
135+
136+
```js
137+
coreui.Popover.getOrCreateInstance(myPopoverEl, configObject)
94138
```
95139

96-
If you'd like to get a particular plugin instance, each plugin exposes a `getInstance` method. In order to retrieve it directly from an element, do this: `coreui.Popover.getInstance(myPopoverEl)`.
140+
In case an instance wasn't initialized, it may accept and use an optional configuration object as second argument.
97141

98142
### CSS selectors in constructors
99143

100-
You can also use a CSS selector as the first argument instead of a DOM element to initialize the plugin. Currently the element for the plugin is found by the `querySelector` method since our plugins support a single element only.
144+
In addition to the `getInstance` and `getOrCreateInstance` methods, all plugin constructors can accept a DOM element or a valid [CSS selector](#selectors) as the first argument. Plugin elements are found with the `querySelector` method since our plugins only support a single element.
101145

102146
```js
103147
const modal = new coreui.Modal('#myModal')
104148
const dropdown = new coreui.Dropdown('[data-coreui-toggle="dropdown"]')
149+
const offcanvas = coreui.Offcanvas.getInstance('#myOffcanvas')
150+
const alert = coreui.Alert.getOrCreateInstance('#myAlert')
105151
```
106152

107153
### Asynchronous functions and transitions
@@ -132,84 +178,55 @@ carousel.to('1') // Will start sliding to the slide 1 and returns to the caller
132178
carousel.to('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!
133179
```
134180

135-
### Default settings
136181

137-
You can change the default settings for a plugin by modifying the plugin's `Constructor.Default` object:
182+
#### `dispose` method
138183

139-
```js
140-
// changes default for the modal plugin's `keyboard` option to false
141-
coreui.Modal.Default.keyboard = false
142-
```
143-
144-
## No conflict (only if you use jQuery)
145-
146-
Sometimes it is necessary to use CoreUI for Bootstrap plugins with other UI frameworks. In these circumstances, namespace collisions can occasionally occur. If this happens, you may call `.noConflict` on the plugin you wish to revert the value of.
184+
While it may seem correct to use the `dispose` method immediately after `hide()`, it will lead to incorrect results. Here's an example of the problem use:
147185

148186
```js
149-
const coreuiButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
150-
$.fn.coreuiBtn = coreuiButton // give $().coreuiBtn the CoreUI for Bootstrap functionality
187+
const myModal = document.querySelector('#myModal')
188+
myModal.hide() // it is asynchronous
189+
190+
myModal.addEventListener('shown.coreui.hidden', event => {
191+
myModal.dispose()
192+
})
151193
```
152194

153-
## Version numbers
195+
### Default settings
154196

155-
The version of each of CoreUI's plugins can be accessed via the `VERSION` property of the plugin's constructor. For example, for the tooltip plugin:
197+
You can change the default settings for a plugin by modifying the plugin's `Constructor.Default` object:
156198

157199
```js
158-
coreui.Tooltip.VERSION // => "{{< param current_version >}}"
200+
// changes default for the modal plugin's `keyboard` option to false
201+
coreui.Modal.Default.keyboard = false
159202
```
160203

161-
## No special fallbacks when JavaScript is disabled
204+
## Methods and properties
162205

163-
CoreUI's plugins don't fall back particularly gracefully when JavaScript is disabled. If you care about the user experience in this case, use [`<noscript>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript) to explain the situation (and how to re-enable JavaScript) to your users, and/or add your own custom fallbacks.
206+
Every CoreUI for Bootstrap plugin exposes the following methods and static properties.
164207

165-
{{< callout warning >}}
166-
##### Third-party libraries
208+
{{< bs-table "table" >}}
209+
| Method | Description |
210+
| --- | --- |
211+
| `dispose` | Destroys an element's modal. (Removes stored data on the DOM element) |
212+
| `getInstance` | *Static* method which allows you to get the modal instance associated with a DOM element. |
213+
| `getOrCreateInstance` | *Static* method which allows you to get the modal instance associated with a DOM element, or create a new one in case it wasn't initialized. |
214+
{{< /bs-table >}}
167215

168-
**CoreUI for Bootstrap does not officially support third-party JavaScript libraries** like Prototype or jQuery UI. Despite `.noConflict` and namespaced events, there may be compatibility problems that you need to fix on your own.
169-
{{< /callout >}}
216+
{{< bs-table "table" >}}
217+
| Static property | Description |
218+
| --- | --- |
219+
| `NAME` | Returns the plugin name. (Example: `coreui.Tooltip.NAME`) |
220+
| `VERSION` | The version of each of CoreUI for Bootstrap's plugins can be accessed via the `VERSION` property of the plugin's constructor (Example: `coreui.Tooltip.VERSION`) |
221+
{{< /bs-table >}}
170222

171223
## Sanitizer
172224

173225
Tooltips and Popovers use our built-in sanitizer to sanitize options which accept HTML.
174226

175227
The default `allowList` value is the following:
176228

177-
```js
178-
const ARIA_ATTRIBUTE_PATTERN = /^aria-[\w-]*$/i
179-
const DefaultAllowlist = {
180-
// Global attributes allowed on any supplied element below.
181-
'*': ['class', 'dir', 'id', 'lang', 'role', ARIA_ATTRIBUTE_PATTERN],
182-
a: ['target', 'href', 'title', 'rel'],
183-
area: [],
184-
b: [],
185-
br: [],
186-
col: [],
187-
code: [],
188-
div: [],
189-
em: [],
190-
hr: [],
191-
h1: [],
192-
h2: [],
193-
h3: [],
194-
h4: [],
195-
h5: [],
196-
h6: [],
197-
i: [],
198-
img: ['src', 'srcset', 'alt', 'title', 'width', 'height'],
199-
li: [],
200-
ol: [],
201-
p: [],
202-
pre: [],
203-
s: [],
204-
small: [],
205-
span: [],
206-
sub: [],
207-
sup: [],
208-
strong: [],
209-
u: [],
210-
ul: []
211-
}
212-
```
229+
{{< js-docs name="allow-list" file="js/src/util/sanitizer.js" >}}
213230

214231
If you want to add new values to this default `allowList` you can do the following:
215232

@@ -231,10 +248,55 @@ myDefaultAllowList['*'].push(myCustomRegex)
231248
If you want to bypass our sanitizer because you prefer to use a dedicated library, for example [DOMPurify](https://www.npmjs.com/package/dompurify), you should do the following:
232249

233250
```js
234-
const yourTooltipEl = document.getElementById('yourTooltip')
251+
const yourTooltipEl = document.querySelector('#yourTooltip')
235252
const tooltip = new coreui.Tooltip(yourTooltipEl, {
236253
sanitizeFn(content) {
237254
return DOMPurify.sanitize(content)
238255
}
239256
})
240257
```
258+
259+
## Optionally using jQuery
260+
261+
**You don't need jQuery in CoreUI for Bootstrap**, but it's still possible to use our components with jQuery. If CoreUI for Bootstrap detects `jQuery` in the `window` object, it'll add all of our components in jQuery's plugin system. This allows you to do the following:
262+
263+
```js
264+
// to enable tooltips with the default configuration
265+
$('[data-coreui-toggle="tooltip"]').tooltip()
266+
267+
// to initialize tooltips with given configuration
268+
$('[data-coreui-toggle="tooltip"]').tooltip({
269+
boundary: 'clippingParents',
270+
customClass: 'myClass'
271+
})
272+
273+
// to trigger the `show` method
274+
$('#myTooltip').tooltip('show')
275+
```
276+
277+
The same goes for our other components.
278+
279+
### No conflict
280+
281+
Sometimes it is necessary to use CoreUI for Bootstrap plugins with other UI frameworks. In these circumstances, namespace collisions can occasionally occur. If this happens, you may call `.noConflict` on the plugin you wish to revert the value of.
282+
283+
```js
284+
const coreuiButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
285+
$.fn.coreuiBtn = coreuiButton // give $().coreuiBtn the CoreUI for Bootstrap functionality
286+
```
287+
288+
CoreUI for Bootstrap does not officially support third-party JavaScript libraries like Prototype or jQuery UI. Despite `.noConflict` and namespaced events, there may be compatibility problems that you need to fix on your own.
289+
290+
### jQuery events
291+
292+
CoreUI for Bootstrap will detect jQuery if `jQuery` is present in the `window` object and there is no `data-coreui-no-jquery` attribute set on `<body>`. If jQuery is found, CoreUI for Bootstrap will emit events thanks to jQuery's event system. So if you want to listen to CoreUI for Bootstrap's events, you'll have to use the jQuery methods (`.on`, `.one`) instead of `addEventListener`.
293+
294+
```js
295+
$('#myTab a').on('shown.coreui.tab', () => {
296+
// do something...
297+
})
298+
```
299+
300+
## Disabled JavaScript
301+
302+
CoreUI for Bootstrap's plugins have no special fallback when JavaScript is disabled. If you care about the user experience in this case, use [`<noscript>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript) to explain the situation (and how to re-enable JavaScript) to your users, and/or add your own custom fallbacks.

hugo.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ params:
8383
js_hash: "sha384-SNBINhaW16aFRorhuZgq6f2m9//dBqdXtCIYH828JLS9m/DkGi9K3xoQzGkZeWxb"
8484
js_bundle: "https://cdn.jsdelivr.net/npm/@coreui/[email protected]/dist/js/coreui.bundle.min.js"
8585
js_bundle_hash: "sha384-4rXehlv2uFB3REIcn+690wKP8X6f8/p7Y+fr+C/N1ZTZ7FuSBdjRKGeSQQgBy7zO"
86-
popper: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
87-
popper_hash: "sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
86+
popper: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
87+
popper_hash: "sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
88+
popper_esm: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/esm/popper.min.js"
8889

8990
anchors:
9091
min: 2

0 commit comments

Comments
 (0)