Skip to content
Open
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
9 changes: 9 additions & 0 deletions l10n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ msgstr ""
msgid "Pick an emoji"
msgstr ""

msgid "Pick files"
msgstr ""

msgid "Please choose a date"
msgstr ""

Expand Down Expand Up @@ -506,6 +509,12 @@ msgstr ""
msgid "Undo changes"
msgstr ""

msgid "Upload files"
msgstr ""

msgid "Upload folders"
msgstr ""

msgid "User status: {status}"
msgstr ""

Expand Down
259 changes: 259 additions & 0 deletions src/components/NcFilePicker/NcFilePicker.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<docs>
This component allows to pick local files (or folders) which can be used to upload them to Nextcloud.

### Exposed methods

- `function reset(): void`
This method allows to reset the internal state of the file picker to clear the current selection

### Example

```vue
<template>
<div>
<div class="wrapper">
<NcFilePicker ref="picker"
allow-folders
@pick="selectedFiles = $event" />

<NcButton variant="tertiary"
@click="clearPicker">
Clear
</NcButton>
</div>

<h3>Selected files:</h3>
<ul>
<li v-for="file in selectedFiles" key="file.name">
{{ file.webkitRelativePath || file.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
selectedFiles: [],
}
},
methods: {
/**
* This will clear the selected files from the picker.
*/
clearPicker() {
this.$refs.picker.reset()
},
},
}
</script>
<style scoped>
.wrapper {
display: flex;
gap: 10px;
}
</style>
```
</docs>

<script setup lang="ts">
import type { Slot } from 'vue'

import { computed, nextTick, useTemplateRef } from 'vue'
import { t } from '../../l10n.js'
import IconFolderUpload from 'vue-material-design-icons/FolderUpload.vue'
import IconPlus from 'vue-material-design-icons/Plus.vue'
import IconUpload from 'vue-material-design-icons/Upload.vue'
import NcActionButton from '../NcActionButton/index.js'
import NcActions from '../NcActions/index.js'
import NcLoadingIcon from '../NcLoadingIcon/index.js'

const props = withDefaults(defineProps<{
/**
* File types to accept
*/
accept?: string[]

/**
* Allow picking a folder
*/
allowFolders?: boolean

/**
* Disabled state of the picker
*/
disabled?: boolean

/**
* If set then the label is only used for accessibility but not shown visually
*/
iconOnly?: boolean

/**
* Label of the picker
*/
label?: string

/**
* If set then the picker will be set into a loading state.
* This means the picker is disabled, a loading spinner is shown and the label is adjusted.
*/
loading?: boolean

/**
* Can the user pick multiple files
*/
multiple?: boolean

/**
* The variant of the button
*/
variant?: 'primary' | 'secondary' | 'tertiary'
}>(), {
accept: undefined,
label: () => t('Pick files'),
variant: 'primary',
})

const emit = defineEmits<{
pick: [files: File[]]
}>()

defineSlots<{
/**
* Custom NcAction* to be shown within the picker menu
*/
actions?: Slot

/**
* Optional custom icon for the picker menu
*/
icon?: Slot

/**
* Optional content to be shown in the picker.
* This can be used e.g. for a progress bar or similar.
*/
default?: Slot
}>()

defineExpose({
reset,
})

const formElement = useTemplateRef('form')
const inputElement = useTemplateRef('input')

/**
* The current label to be used as menu name and accessible name of the picker.
*/
const currentLabel = computed(() => {
if (props.loading) {
return t('Uploading …')
}
return props.label
})

/**
* Check whether the current browser supports uploading directories
* Hint: This does not check if the current connection supports this, as some browsers require a secure context!
*/
const canUploadFolders = computed(() => {
return props.allowFolders && 'webkitdirectory' in HTMLInputElement.prototype
})

/**
* Trigger file picker
*
* @param uploadFolders - Whether to upload folders or files
*/
function triggerPickFiles(uploadFolders: boolean) {
// Without reset selecting the same file doesn't trigger the change event
reset()

// Only if the browser supports picking folders and the user selected "pick folder" we set the file input to directory picking.
if (canUploadFolders.value) {
inputElement.value!.webkitdirectory = uploadFolders
}

// Wait for the reset and the `webkitdirectory` to be dispatched in DOM
nextTick(() => inputElement.value!.click())
}

/**
* Handle picking some local files
*/
function onPick() {
const files = inputElement.value?.files ? Array.from(inputElement.value.files) : []
emit('pick', files)
}

/**
* Reset the picker state of the currently selected files.
*/
function reset() {
formElement.value!.reset()
}
</script>

<template>
<form ref="form"
class="vue-file-picker">
<NcActions :aria-label="currentLabel"
:disabled="disabled || loading"
:menu-name="iconOnly ? undefined : currentLabel"
:force-name="!iconOnly"
:variant>
<template #icon>
<slot v-if="!loading" name="icon">
<IconPlus :size="20" />
</slot>
<NcLoadingIcon v-else />
</template>

<NcActionButton close-after-click
@click="triggerPickFiles(false)">
<template #icon>
<IconUpload :size="20" />
</template>
{{ t('Upload files') }}
</NcActionButton>

<NcActionButton v-if="canUploadFolders"
close-after-click
@click="triggerPickFiles(true)">
<template #icon>
<IconFolderUpload style="color: var(--color-primary-element)" :size="20" />
</template>
{{ t('Upload folders') }}
</NcActionButton>

<!-- App defined upload actions -->
<slot name="actions" />
Comment on lines +236 to +237
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an option to include default 'apps/files/api/v1/templates' actions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where would this be used?
Files has its own way to register "new"-menu entries and it is not really related to an local-files picker?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere where Filepicker is used to pick a file that is to share, and where it can also be a new file.

Very similar: Files, Talk

image

A bit similar (with a way to select from files, and files templates seem to be missing)

Notes (text):

image

Calendar:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not really related to an local-files picker?

As files are stored in Nextcloud, it often makes sense to also be able to share a file from Nextcloud apart from uploading a new one.

For example, currently 4 different apps have it but name it differently.

Copy link
Contributor

@ShGKme ShGKme Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it makes sense to me to have a shared component for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it makes sense to me to have a shared component for it.

Yes but in this case I wanted to first have a file input component, which we could reuse for that.
Because e.g. my use case in forms does not need this. It only needs to pick files from local computer.

Maybe add an option to include default 'apps/files/api/v1/templates' actions?

But here my question was more like that this is apps specific as the files UI does not use this API but instead uses the actions registered on the frontend:
https://nextcloud-libraries.github.io/nextcloud-files/functions/index.getNewFileMenuEntries.html

</NcActions>

<!-- Hidden files picker input - also hidden for accessibility as otherwise such users also loose the ability to pick files -->
<input ref="input"
:accept="accept?.join(', ')"
aria-hidden="true"
class="hidden-visually"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should have a different class, to be visually hidden, but stays in the same position, like with the hidden input in NcCheckboxRadioSwitch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does it needs to stay in that position? Was not a problem in files and photos

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the same reason as in <NcCheckboxRadioSwitch> - assistive technologies may focus on an actual input. Unlike visually hidden text, it is still a part of the tab sequence. And then, if the input is moved away, focusing it moves scrolling containers and users with assistive technologies far away.

But now, while writing it, I think it makes less sense for the file input because it is never used directly (like a checkbox does).

:multiple
type="file"
@change="onPick">

<slot />
</form>
</template>

<style lang="scss" scoped>
.vue-file-picker {
display: inline-flex;
align-items: center;
height: var(--default-clickable-area);
}
</style>
6 changes: 6 additions & 0 deletions src/components/NcFilePicker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

export { default } from './NcFilePicker.vue'
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export { default as NcDialogButton } from './NcDialogButton/index.ts'
export { default as NcEllipsisedOption } from './NcEllipsisedOption/index.js'
export { default as NcEmojiPicker } from './NcEmojiPicker/index.js'
export { default as NcEmptyContent } from './NcEmptyContent/index.ts'
export { default as NcFilePicker } from './NcFilePicker/index.ts'
export { default as NcGuestContent } from './NcGuestContent/index.ts'
export { default as NcHeaderButton } from './NcHeaderButton/index.ts'
export { default as NcHeaderMenu } from './NcHeaderMenu/index.ts'
Expand Down
Loading