You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -15,6 +15,12 @@ Plugins can be included individually (using CoreUI's individual `js/dist/*.js`),
15
15
16
16
If you use a bundler (Webpack, Rollup...), you can use `/js/dist/*.js` files which are UMD ready.
17
17
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
+
18
24
## Using CoreUI for Bootstrap as a module
19
25
20
26
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
29
35
</script>
30
36
```
31
37
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*asPopperfrom"@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:
34
52
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).
Some plugins and CSS components depend on other plugins. If you include plugins individually, make sure to check for these dependencies in the docs.
41
88
42
89
Our dropdowns, popovers and tooltips also depend on [Popper](https://popper.js.org/).
43
90
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
-
48
91
## Data attributes
49
92
50
93
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.)
51
94
52
-
{{< callout warning >}}
53
95
## Selectors
54
96
55
97
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).
56
98
If you use special selectors, for example: `collapse:Example` be sure to escape them.
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):
80
120
81
121
```js
82
-
$('#myTab a').on('shown.coreui.tab', () => {
83
-
// do something...
84
-
})
85
-
```
86
-
{{< /callout >}}
122
+
constmyModalEl=document.querySelector('#myModal')
123
+
constmodal=newcoreui.Modal(myModalEl) // initialized with defaults
87
124
88
-
## Programmatic API
125
+
constconfigObject= { keyboard:false }
126
+
constmodal1=newcoreui.Modal(myModalEl, configObject) // initialized with no keyboard
127
+
```
89
128
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:
91
130
92
131
```js
93
-
constmyModalEl=document.getElementById('myModal')
132
+
coreui.Popover.getInstance(myPopoverEl)
133
+
```
94
134
95
-
constmodal=newcoreui.Modal(myModalEl) // initialized with defaults
96
-
constmodal1=newcoreui.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.
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.
100
144
101
145
### CSS selectors in constructors
102
146
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.
@@ -135,84 +181,55 @@ carousel.to('1') // Will start sliding to the slide 1 and returns to the caller
135
181
carousel.to('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!
136
182
```
137
183
138
-
### Default settings
139
184
140
-
You can change the default settings for a plugin by modifying the plugin's `Constructor.Default` object:
185
+
#### `dispose` method
141
186
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:
150
188
151
189
```js
152
-
constcoreuiButton=$.fn.button.noConflict() // return $.fn.button to previously assigned value
153
-
$.fn.coreuiBtn= coreuiButton // give $().coreuiBtn the CoreUI for Bootstrap functionality
// changes default for the modal plugin's `keyboard` option to false
204
+
coreui.Modal.Default.keyboard=false
162
205
```
163
206
164
-
## No special fallbacks when JavaScript is disabled
207
+
## Methods and properties
165
208
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.
167
210
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 >}}
170
218
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 >}}
173
225
174
226
## Sanitizer
175
227
176
228
Tooltips and Popovers use our built-in sanitizer to sanitize options which accept HTML.
177
229
178
230
The default `allowList` value is the following:
179
231
180
-
```js
181
-
constARIA_ATTRIBUTE_PATTERN=/^aria-[\w-]*$/i
182
-
constDefaultAllowlist= {
183
-
// Global attributes allowed on any supplied element below.
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:
**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
+
constcoreuiButton=$.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.
0 commit comments