Skip to content

Commit

Permalink
Define global focus trap
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Nov 15, 2022
1 parent 3a51d5b commit dfe69f7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 59 deletions.
50 changes: 15 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"@nextcloud/capabilities": "^1.0.4",
"@nextcloud/dialogs": "^3.1.4",
"@nextcloud/event-bus": "^3.0.0",
"@nextcloud/focus-trap": "^0.1.0-beta",
"@nextcloud/initial-state": "^2.0.0",
"@nextcloud/l10n": "^1.6.0",
"@nextcloud/logger": "^2.2.1",
Expand All @@ -55,6 +54,7 @@
"emoji-mart-vue-fast": "^11.1.1",
"escape-html": "^1.0.3",
"floating-vue": "^1.0.0-beta.18",
"focus-trap": "^7.1.0",
"hammerjs": "^2.0.8",
"linkify-string": "^4.0.0",
"md5": "^2.3.0",
Expand Down
46 changes: 24 additions & 22 deletions src/components/NcModal/NcModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
-->

<docs>

```vue
<template>
<div>
Expand All @@ -38,13 +37,6 @@
</NcModal>
</div>
</template>
<style scoped>
.modal__content {
margin: 50px;
text-align: center;
}
</style>
<script>
export default {
data() {
Expand All @@ -62,6 +54,12 @@ export default {
}
}
</script>
<style scoped>
.modal__content {
margin: 50px;
text-align: center;
}
</style>
```

### Modal with more properties
Expand Down Expand Up @@ -125,9 +123,7 @@ export default {
.input-field {
margin: 12px 0px;
}
</style>

```

### Usage of popover in modal
Expand All @@ -145,12 +141,6 @@ export default {
</NcModal>
</div>
</template>
<style scoped>
.modal__content {
margin: 50px;
text-align: center;
}
</style>
<script>
export default {
data() {
Expand All @@ -172,6 +162,12 @@ export default {
},
}
</script>
<style scoped>
.modal__content {
margin: 50px;
text-align: center;
}
</style>
```
</docs>

Expand Down Expand Up @@ -310,22 +306,23 @@ export default {
</template>

<script>
import NcActions from '../NcActions/index.js'
import Tooltip from '../../directives/Tooltip/index.js'
import l10n from '../../mixins/l10n.js'
import Timer from '../../utils/Timer.js'
import { t } from '../../l10n.js'
import NcButton from '../../components/NcButton/index.js'
import GenRandomId from '../../utils/GenRandomId.js'
import l10n from '../../mixins/l10n.js'
import NcActions from '../NcActions/index.js'
import NcButton from '../../components/NcButton/index.js'
import Timer from '../../utils/Timer.js'
import Tooltip from '../../directives/Tooltip/index.js'
import ChevronLeft from 'vue-material-design-icons/ChevronLeft.vue'
import ChevronRight from 'vue-material-design-icons/ChevronRight.vue'
import Close from 'vue-material-design-icons/Close.vue'
import Pause from 'vue-material-design-icons/Pause.vue'
import Play from 'vue-material-design-icons/Play.vue'
import { createFocusTrap } from 'focus-trap'
import Hammer from 'hammerjs'
import { createFocusTrap } from '@nextcloud/focus-trap'
export default {
name: 'NcModal',
Expand Down Expand Up @@ -689,8 +686,13 @@ export default {
const contentContainer = this.$refs.mask
// wait until all children are mounted and available in the DOM before focusTrap can be added
this.$nextTick(() => {
// Create global stack if undefined
Object.assign(window, { _nc_focus_trap: window._nc_focus_trap || [] })
// Init focus trap
this.focusTrap = createFocusTrap(contentContainer, {
allowOutsideClick: true,
trapStack: window._nc_focus_trap,
})
this.focusTrap.activate()
})
Expand Down
7 changes: 6 additions & 1 deletion src/components/NcPopover/NcPopover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ The prop `:focus-trap="false"` help to prevent it when the default behavior is n

<script>
import { Dropdown } from 'floating-vue'
import { createFocusTrap } from '@nextcloud/focus-trap'
import { createFocusTrap } from 'focus-trap'
export default {
name: 'NcPopover',
Expand Down Expand Up @@ -155,11 +155,16 @@ export default {
return
}
// Create global stack if undefined
Object.assign(window, { _nc_focus_trap: window._nc_focus_trap || [] })
// Init focus trap
this.$focusTrap = createFocusTrap(el, {
// Prevents to lose focus using esc key
// Focus will be release when popover be hide
escapeDeactivates: false,
allowOutsideClick: true,
trapStack: window._nc_focus_trap,
})
this.$focusTrap.activate()
},
Expand Down

0 comments on commit dfe69f7

Please sign in to comment.