-
Notifications
You must be signed in to change notification settings - Fork 96
feat(NcFilePicker): add picker component to select local files #7097
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" /> | ||
</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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the same reason as in 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> |
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' |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
A bit similar (with a way to select from files, and files templates seem to be missing)
Notes (text):
Calendar:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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