-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): try to guess bs version (#2580)
* feat(core): try to guess bs version * chore(build): fix typings * fix(core): fix version guess function
- Loading branch information
Showing
3 changed files
with
33 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,38 @@ | ||
import { window } from './facade/browser'; | ||
|
||
let guessedVersion: 'bs3' | 'bs4'; | ||
|
||
function _guessBsVersion(): 'bs3' | 'bs4' { | ||
if (typeof document === 'undefined') { | ||
return null; | ||
} | ||
const spanEl = document.createElement('span'); | ||
spanEl.innerText = 'test bs version'; | ||
document.body.appendChild(spanEl); | ||
spanEl.classList.add('d-none'); | ||
const rect = spanEl.getBoundingClientRect(); | ||
document.body.removeChild(spanEl); | ||
if (!rect) { | ||
return 'bs3'; | ||
} | ||
|
||
return rect.top === 0 ? 'bs4' : 'bs3'; | ||
} | ||
|
||
// todo: in ngx-bootstrap, bs4 will became a default one | ||
export function isBs3(): boolean { | ||
if (typeof window === 'undefined') { | ||
return true; | ||
} | ||
|
||
if (typeof window.__theme === 'undefined') { | ||
if (guessedVersion) { | ||
return guessedVersion === 'bs3'; | ||
} | ||
guessedVersion = _guessBsVersion(); | ||
|
||
return guessedVersion === 'bs3'; | ||
} | ||
|
||
return window.__theme !== 'bs4'; | ||
} |