forked from facebook/react-native-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to initialize snackplayer after navigation and tab changes
- Loading branch information
Showing
3 changed files
with
73 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const path = require('path'); | ||
|
||
// TODO this plugin could be removed after PR merged: | ||
// https://github.com/facebook/docusaurus/pull/3545 | ||
module.exports = function() { | ||
return { | ||
plugin: 'site-plugin', | ||
getClientModules() { | ||
return [path.resolve(__dirname, './snackPlayerInitializer')]; | ||
}, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; | ||
|
||
export default (function() { | ||
if (!ExecutionEnvironment.canUseDOM) { | ||
return null; | ||
} | ||
|
||
function initSnackPlayers() { | ||
console.log('initSnackPlayers'); | ||
window.ExpoSnack.initialize(); | ||
} | ||
|
||
function setupTabPanelsMutationObservers() { | ||
const tabPanels = document.querySelectorAll('[role=tabpanel]'); | ||
console.log('setupTabPanelsMutationObservers', {tabPanels}); | ||
tabPanels.forEach(tabPanel => { | ||
new MutationObserver(function(mutations, observer) { | ||
initSnackPlayers(); | ||
console.log('tabPanel MutationObserver triggered', {tabPanels}); | ||
}).observe(tabPanel, {childList: true}); | ||
}); | ||
} | ||
|
||
if (document.readyState === 'complete') { | ||
setupTabPanelsMutationObservers(); | ||
} else { | ||
document.addEventListener('readystatechange', function() { | ||
if (document.readyState === 'complete') { | ||
setupTabPanelsMutationObservers(); | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
onRouteUpdate({location}) { | ||
console.log('onRouteUpdate', {location}); | ||
|
||
// TODO temporary, because onRouteUpdate fires before the new route renders... | ||
// see https://github.com/facebook/docusaurus/issues/3399#issuecomment-704401189 | ||
setTimeout(() => { | ||
initSnackPlayers(); | ||
setupTabPanelsMutationObservers(); | ||
}, 0); | ||
}, | ||
}; | ||
})(); |