Skip to content

Commit 84f09e4

Browse files
authored
feat(core): try to guess bs version (valor-software#2580)
* feat(core): try to guess bs version * chore(build): fix typings * fix(core): fix version guess function
1 parent 363bb22 commit 84f09e4

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

demo/src/index-bs4.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
</script>
3939

4040
<script src="assets/js/prettify.min.js"></script>
41-
<!-- Enable bootstrap 4 theme -->
42-
<script>window.__theme = 'bs4';</script>
4341
<!-- script files will be inserted by html-webpack-plugin -->
4442
</body>
4543
</html>

demo/src/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<!--<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">-->
1919

2020
<!--link to bootstrap.css-->
21-
<!--<script>window.__theme = 'bs4';</script>-->
2221
<!--<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet">-->
2322
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
2423
<link rel="stylesheet" href="assets/css/style.css">

src/utils/ng2-bootstrap-config.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
import { window } from './facade/browser';
22

3+
let guessedVersion: 'bs3' | 'bs4';
4+
5+
function _guessBsVersion(): 'bs3' | 'bs4' {
6+
if (typeof document === 'undefined') {
7+
return null;
8+
}
9+
const spanEl = document.createElement('span');
10+
spanEl.innerText = 'test bs version';
11+
document.body.appendChild(spanEl);
12+
spanEl.classList.add('d-none');
13+
const rect = spanEl.getBoundingClientRect();
14+
document.body.removeChild(spanEl);
15+
if (!rect) {
16+
return 'bs3';
17+
}
18+
19+
return rect.top === 0 ? 'bs4' : 'bs3';
20+
}
21+
22+
// todo: in ngx-bootstrap, bs4 will became a default one
323
export function isBs3(): boolean {
24+
if (typeof window === 'undefined') {
25+
return true;
26+
}
27+
28+
if (typeof window.__theme === 'undefined') {
29+
if (guessedVersion) {
30+
return guessedVersion === 'bs3';
31+
}
32+
guessedVersion = _guessBsVersion();
33+
34+
return guessedVersion === 'bs3';
35+
}
36+
437
return window.__theme !== 'bs4';
538
}

0 commit comments

Comments
 (0)