Skip to content

Commit 999f775

Browse files
committed
Merge branch 'v4-next' of https://github.com/coreui/coreui into v4-next
2 parents 162d0fb + 4a23755 commit 999f775

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
@@ -15,6 +15,12 @@ Plugins can be included individually (using CoreUI's individual `js/dist/*.js`),
1515

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

18+
## Usage with JavaScript frameworks
19+
20+
- Angular: [CoreUI for Angular](https://coreui.io/angular/docs/getting-started/introduction)
21+
- React: [CoreUI for React](https://coreui.io/react/docs/getting-started/introduction/)
22+
- Vue: [CoreUI for Vue](https://coreui.io/vue/docs/getting-started/introduction.html)
23+
1824
## Using CoreUI for Bootstrap as a module
1925

2026
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).
@@ -29,32 +35,67 @@ We provide a version of CoreUI for Bootstrap built as `ESM` (`coreui.esm.js` and
2935
</script>
3036
```
3137

32-
{{< callout warning >}}
33-
## Incompatible plugins
38+
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:
39+
40+
<!-- eslint-skip -->
41+
```js
42+
import * as Popper from "@popperjs/core"
43+
```
44+
45+
If you try this as-is, you'll see an error in the console like the following:
46+
47+
```text
48+
Uncaught TypeError: Failed to resolve module specifier "@popperjs/core". Relative references must start with either "/", "./", or "../".
49+
```
50+
51+
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:
3452

35-
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).
36-
{{< /callout >}}
53+
<!-- eslint-skip -->
54+
```html
55+
<!doctype html>
56+
<html lang="en">
57+
<head>
58+
<meta charset="utf-8">
59+
<meta name="viewport" content="width=device-width, initial-scale=1">
60+
<link href="{{< param "cdn.css" >}}" rel="stylesheet" integrity="{{< param "cdn.css_hash" >}}" crossorigin="anonymous">
61+
<title>Hello, modularity!</title>
62+
</head>
63+
<body>
64+
<h1>Hello, modularity!</h1>
65+
<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>
66+
67+
<script async src="https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.min.js" crossorigin="anonymous"></script>
68+
<script type="importmap">
69+
{
70+
"imports": {
71+
"@popperjs/core": "{{< param "cdn.popper_esm" >}}",
72+
"coreui": "https://cdn.jsdelivr.net/npm/coreui@{{< param "current_version" >}}/dist/js/coreui.esm.min.js"
73+
}
74+
}
75+
</script>
76+
<script type="module">
77+
import * as coreui from '@coreui/coreui'
78+
79+
new coreui.Popover(document.getElementById('popoverButton'))
80+
</script>
81+
</body>
82+
</html>
83+
```
3784

3885
## Dependencies
3986

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

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

44-
## Still want to use jQuery? It's possible!
45-
46-
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.
47-
4891
## Data attributes
4992

5093
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.)
5194

52-
{{< callout warning >}}
5395
## Selectors
5496

5597
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).
5698
If you use special selectors, for example: `collapse:Example` be sure to escape them.
57-
{{< /callout >}}
5899

59100
## Events
60101

@@ -73,38 +114,43 @@ myModal.addEventListener('show.coreui.modal', event => {
73114
})
74115
```
75116

76-
{{< callout warning >}}
77-
## jQuery events
117+
## Programmatic API
78118

79-
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`.
119+
All constructors accept an optional options object or nothing (which initiates a plugin with its default behavior):
80120

81121
```js
82-
$('#myTab a').on('shown.coreui.tab', () => {
83-
// do something...
84-
})
85-
```
86-
{{< /callout >}}
122+
const myModalEl = document.querySelector('#myModal')
123+
const modal = new coreui.Modal(myModalEl) // initialized with defaults
87124

88-
## Programmatic API
125+
const configObject = { keyboard: false }
126+
const modal1 = new coreui.Modal(myModalEl, configObject) // initialized with no keyboard
127+
```
89128

90-
All constructors accept an optional options object or nothing (which initiates a plugin with its default behavior):
129+
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:
91130

92131
```js
93-
const myModalEl = document.getElementById('myModal')
132+
coreui.Popover.getInstance(myPopoverEl)
133+
```
94134

95-
const modal = new coreui.Modal(myModalEl) // initialized with defaults
96-
const modal1 = new coreui.Modal(myModalEl, { keyboard: false }) // initialized with no keyboard
135+
This method will return `null` if an instance is not initiated over the requested element.
136+
137+
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.
138+
139+
```js
140+
coreui.Popover.getOrCreateInstance(myPopoverEl, configObject)
97141
```
98142

99-
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)`.
143+
In case an instance wasn't initialized, it may accept and use an optional configuration object as second argument.
100144

101145
### CSS selectors in constructors
102146

103-
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.
147+
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.
104148

105149
```js
106150
const modal = new coreui.Modal('#myModal')
107151
const dropdown = new coreui.Dropdown('[data-coreui-toggle="dropdown"]')
152+
const offcanvas = coreui.Offcanvas.getInstance('#myOffcanvas')
153+
const alert = coreui.Alert.getOrCreateInstance('#myAlert')
108154
```
109155

110156
### Asynchronous functions and transitions
@@ -135,84 +181,55 @@ carousel.to('1') // Will start sliding to the slide 1 and returns to the caller
135181
carousel.to('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!
136182
```
137183

138-
### Default settings
139184

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

142-
```js
143-
// changes default for the modal plugin's `keyboard` option to false
144-
coreui.Modal.Default.keyboard = false
145-
```
146-
147-
## No conflict (only if you use jQuery)
148-
149-
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.
187+
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:
150188

151189
```js
152-
const coreuiButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
153-
$.fn.coreuiBtn = coreuiButton // give $().coreuiBtn the CoreUI for Bootstrap functionality
190+
const myModal = document.querySelector('#myModal')
191+
myModal.hide() // it is asynchronous
192+
193+
myModal.addEventListener('shown.coreui.hidden', event => {
194+
myModal.dispose()
195+
})
154196
```
155197

156-
## Version numbers
198+
### Default settings
157199

158-
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:
200+
You can change the default settings for a plugin by modifying the plugin's `Constructor.Default` object:
159201

160202
```js
161-
coreui.Tooltip.VERSION // => "{{< param current_version >}}"
203+
// changes default for the modal plugin's `keyboard` option to false
204+
coreui.Modal.Default.keyboard = false
162205
```
163206

164-
## No special fallbacks when JavaScript is disabled
207+
## Methods and properties
165208

166-
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.
209+
Every CoreUI for Bootstrap plugin exposes the following methods and static properties.
167210

168-
{{< callout warning >}}
169-
##### Third-party libraries
211+
{{< bs-table "table" >}}
212+
| Method | Description |
213+
| --- | --- |
214+
| `dispose` | Destroys an element's modal. (Removes stored data on the DOM element) |
215+
| `getInstance` | *Static* method which allows you to get the modal instance associated with a DOM element. |
216+
| `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. |
217+
{{< /bs-table >}}
170218

171-
**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.
172-
{{< /callout >}}
219+
{{< bs-table "table" >}}
220+
| Static property | Description |
221+
| --- | --- |
222+
| `NAME` | Returns the plugin name. (Example: `coreui.Tooltip.NAME`) |
223+
| `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`) |
224+
{{< /bs-table >}}
173225

174226
## Sanitizer
175227

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

178230
The default `allowList` value is the following:
179231

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

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

@@ -234,10 +251,55 @@ myDefaultAllowList['*'].push(myCustomRegex)
234251
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:
235252

236253
```js
237-
const yourTooltipEl = document.getElementById('yourTooltip')
254+
const yourTooltipEl = document.querySelector('#yourTooltip')
238255
const tooltip = new coreui.Tooltip(yourTooltipEl, {
239256
sanitizeFn(content) {
240257
return DOMPurify.sanitize(content)
241258
}
242259
})
243260
```
261+
262+
## Optionally using jQuery
263+
264+
**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:
265+
266+
```js
267+
// to enable tooltips with the default configuration
268+
$('[data-coreui-toggle="tooltip"]').tooltip()
269+
270+
// to initialize tooltips with given configuration
271+
$('[data-coreui-toggle="tooltip"]').tooltip({
272+
boundary: 'clippingParents',
273+
customClass: 'myClass'
274+
})
275+
276+
// to trigger the `show` method
277+
$('#myTooltip').tooltip('show')
278+
```
279+
280+
The same goes for our other components.
281+
282+
### No conflict
283+
284+
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.
285+
286+
```js
287+
const coreuiButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
288+
$.fn.coreuiBtn = coreuiButton // give $().coreuiBtn the CoreUI for Bootstrap functionality
289+
```
290+
291+
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.
292+
293+
### jQuery events
294+
295+
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`.
296+
297+
```js
298+
$('#myTab a').on('shown.coreui.tab', () => {
299+
// do something...
300+
})
301+
```
302+
303+
## Disabled JavaScript
304+
305+
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
@@ -86,8 +86,9 @@ params:
8686
js_hash: "sha384-SNBINhaW16aFRorhuZgq6f2m9//dBqdXtCIYH828JLS9m/DkGi9K3xoQzGkZeWxb"
8787
js_bundle: "https://cdn.jsdelivr.net/npm/@coreui/[email protected]/dist/js/coreui.bundle.min.js"
8888
js_bundle_hash: "sha384-4rXehlv2uFB3REIcn+690wKP8X6f8/p7Y+fr+C/N1ZTZ7FuSBdjRKGeSQQgBy7zO"
89-
popper: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
90-
popper_hash: "sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
89+
popper: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
90+
popper_hash: "sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
91+
popper_esm: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/esm/popper.min.js"
9192
css_pro: "https://cdn.jsdelivr.net/npm/@coreui/[email protected]/dist/css/coreui.min.css"
9293
css_pro_hash: "sha384-/sHcsof/cb82oVJQ5XJEIc705z0Qi5H76RNLFZSz0QAfGwCEjo1Rol/F/BkYk0rr"
9394
css_pro_rtl: "https://cdn.jsdelivr.net/npm/@coreui/[email protected]/dist/css/coreui.rtl.min.css"

0 commit comments

Comments
 (0)