Skip to content

Commit

Permalink
Fixed problem when usage navigation by modals
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Mar 29, 2024
1 parent c5616b4 commit 7a1bd52
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"isEntry": true
},
"resources/js/app.js": {
"file": "assets/app-B-hWsIbj.js",
"file": "assets/app-wv8uX38s.js",
"src": "resources/js/app.js",
"isEntry": true,
"css": [
Expand Down
1 change: 1 addition & 0 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ application.register('textarea-autogrow', TextareaAutogrow);

import './initializers/service-worker';
import './initializers/yandex-metrika.js';
import './initializers/modals.js';
49 changes: 49 additions & 0 deletions resources/js/initializers/modals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Modal } from 'bootstrap';

document.addEventListener('turbo:load', () => {
document.querySelectorAll('.modal').forEach((modal) => {
/**
* Autofocus on modal.
*/
modal.addEventListener('shown.bs.modal', () => {
if (modal.querySelector('[autofocus]') !== null) {
modal.querySelector('[autofocus]').focus();
}
});

modal.addEventListener('hidden.bs.modal', () => {
setTimeout(() => {
document.activeElement?.blur();
});
});
});
});

document.addEventListener('turbo:before-cache', () => {
// Check if the body has the 'modal-open' class
if (document.body.classList.contains('modal-open')) {
if (window.location.href.indexOf('begin') > -1) {
return;
}

// Find the currently visible modal
const element = document.querySelector('.modal.show');

// Get or create the modal instance
const modal = Modal.getOrCreateInstance(element);

// Remove the 'fade' class from the modal element
element.classList.remove('fade');

// Disable animation for the modal backdrop
modal._backdrop._config.isAnimated = false;

// Hide the modal
modal.hide();

// Dispose the modal instance https://getbootstrap.com/docs/5.3/getting-started/javascript/#dispose-method
// IT hack!
modal.dispose();
element.classList.add('fade');
}
});
1 change: 1 addition & 0 deletions resources/views/docs/docs.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class="{{ active(url($link['href']), 'active', 'link-body-emphasis') }} d-inline
data-action="input->search-docs#search"
name="text"
autocomplete="false"
autofocus
data-search-docs-target="text"
type="search"
placeholder="Введите термин или фразу ..."
Expand Down

0 comments on commit 7a1bd52

Please sign in to comment.