Skip to content

refactor(NcActions): use provide/inject instead of $parent search #6705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/NcActionButton/NcActionButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ import ChevronRightIcon from 'vue-material-design-icons/ChevronRight.vue'
import ChevronLeftIcon from 'vue-material-design-icons/ChevronLeft.vue'
import ActionTextMixin from '../../mixins/actionText.js'
import { isRtl } from '../../utils/rtl.ts'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

/**
* Button component to be used in Actions
Expand All @@ -389,7 +390,7 @@ export default {

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/NcActionButtonGroup/NcActionButtonGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default {
import { defineComponent } from 'vue'
import { createElementId } from '../../utils/createElementId.ts'
import { t } from '../../l10n.js'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

/**
* A wrapper for allowing inlining NcAction components within the action menu
Expand All @@ -98,7 +99,7 @@ export default defineComponent({

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/components/NcActionCaption/NcActionCaption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ This component is made to be used inside of the [NcActions](#NcActions) componen
</template>

<script>
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

export default {
name: 'NcActionCaption',

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/NcActionCheckbox/NcActionCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This component is made to be used inside of the [NcActions](#NcActions) componen
<script>
import ActionGlobalMixin from '../../mixins/actionGlobal.js'
import { createElementId } from '../../utils/createElementId.ts'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

export default {
name: 'NcActionCheckbox',
Expand All @@ -48,7 +49,7 @@ export default {

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/NcActionLink/NcActionLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default {

<script>
import ActionTextMixin from '../../mixins/actionText.js'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

export default {
name: 'NcActionLink',
Expand All @@ -117,7 +118,7 @@ export default {

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/NcActionRadio/NcActionRadio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ So that only one of each name set can be selected at the same time.
import { useModel } from 'vue'
import { createElementId } from '../../utils/createElementId.ts'
import ActionGlobalMixin from '../../mixins/actionGlobal.js'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

export default {
name: 'NcActionRadio',
Expand All @@ -77,7 +78,7 @@ export default {

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/NcActionRouter/NcActionRouter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

<script>
import ActionTextMixin from '../../mixins/actionText.js'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

export default {
name: 'NcActionRouter',
Expand All @@ -58,7 +59,7 @@ export default {

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/NcActionText/NcActionText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

<script>
import ActionTextMixin from '../../mixins/actionText.js'
import { NC_ACTIONS_IS_SEMANTIC_MENU } from '../NcActions/useNcActions.ts'

export default {
name: 'NcActionText',
Expand All @@ -54,7 +55,7 @@ export default {

inject: {
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
from: NC_ACTIONS_IS_SEMANTIC_MENU,
default: false,
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/components/NcActions/NcActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@
import IconDotsHorizontal from 'vue-material-design-icons/DotsHorizontal.vue'
import NcButton from '../NcButton/index.ts'
import NcPopover from '../NcPopover/index.js'
import { NC_ACTIONS_CLOSE_MENU, NC_ACTIONS_IS_SEMANTIC_MENU } from './useNcActions.ts'

const focusableSelector = '.focusable'

Expand Down Expand Up @@ -958,7 +959,8 @@
* Provide the role for NcAction* components in the NcActions content.
* @type {import('vue').ComputedRef<boolean>}
*/
'NcActions:isSemanticMenu': computed(() => this.actionsMenuSemanticType === 'menu'),
[NC_ACTIONS_IS_SEMANTIC_MENU]: computed(() => this.actionsMenuSemanticType === 'menu'),
[NC_ACTIONS_CLOSE_MENU]: this.closeMenu,
}
},

Expand Down Expand Up @@ -1408,7 +1410,7 @@
},

/**
* @return {NodeListOf<HTMLElement>}

Check warning on line 1413 in src/components/NcActions/NcActions.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The type 'NodeListOf' is undefined
*/
getFocusableMenuItemElements() {
return this.$refs.menu.querySelectorAll(focusableSelector)
Expand Down
11 changes: 11 additions & 0 deletions src/components/NcActions/useNcActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { InjectionKey, ComputedRef } from 'vue'

export const NC_ACTIONS_IS_SEMANTIC_MENU: InjectionKey<ComputedRef<boolean>> = Symbol.for('NcActions:isSemanticMenu')
export const NC_ACTIONS_CLOSE_MENU: InjectionKey<(returnFocus: boolean) => void> = Symbol.for('NcActions:closeMenu')

// TODO: once NcActions migrated from mixins - add composables to created and use NcAction provider/context
13 changes: 8 additions & 5 deletions src/mixins/actionText.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import actionGlobal from './actionGlobal.js'
import { getParent } from '../utils/getParent.ts'
import { NC_ACTIONS_CLOSE_MENU } from '../components/NcActions/useNcActions.ts'

export default {
mixins: [actionGlobal],
Expand Down Expand Up @@ -55,6 +55,12 @@ export default {
},
},

inject: {
closeMenu: {
from: NC_ACTIONS_CLOSE_MENU,
},
},

emits: [
'click',
],
Expand Down Expand Up @@ -83,10 +89,7 @@ export default {
this.$emit('click', event)

if (this.closeAfterClick) {
const parent = getParent(this, 'NcActions')
if (parent && parent.closeMenu) {
parent.closeMenu(false)
}
this.closeMenu(false)
}
},
},
Expand Down
22 changes: 0 additions & 22 deletions src/utils/getParent.ts

This file was deleted.

93 changes: 0 additions & 93 deletions tests/unit/utils/getParent.spec.ts

This file was deleted.

Loading