-
Notifications
You must be signed in to change notification settings - Fork 94
[DRAFT] feat: add NcFormBoxSelectNative #7838
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
Open
ShGKme
wants to merge
2
commits into
main
Choose a base branch
from
feat/NcFormBoxSelectNative
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/components/NcFormBoxSelectNative/NcFormBoxSelectNative.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| <!-- | ||
| - SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| - SPDX-License-Identifier: AGPL-3.0-or-later | ||
| --> | ||
|
|
||
| <script setup lang="ts" generic="T extends string"> | ||
| import { mdiUnfoldMoreHorizontal } from '@mdi/js' | ||
| import { computed, useTemplateRef } from 'vue' | ||
| import NcFormBoxItem from '../NcFormBox/NcFormBoxItem.vue' | ||
| import NcIconSvgWrapper from '../NcIconSvgWrapper/NcIconSvgWrapper.vue' | ||
| import { createElementId } from '../../utils/createElementId.ts' | ||
|
|
||
| /** Selected value */ | ||
| const modelValue = defineModel<T>({ required: true }) | ||
|
|
||
| const { | ||
| label = undefined, | ||
| options, | ||
| disabled = false, | ||
| } = defineProps<{ | ||
| /** Main label */ | ||
| label?: string | ||
| /** Select options */ | ||
| options: { | ||
| label: string | ||
| value: T | ||
| }[] | ||
| /** Disabled state */ | ||
| disabled?: boolean | ||
| }>() | ||
|
|
||
| const selectId = createElementId() | ||
| const selectElement = useTemplateRef('select') | ||
|
|
||
| const selectedLabel = computed(() => options.find((option) => option.value === modelValue.value)?.label) | ||
|
|
||
| // .showPicker() is not available some browsers (e.g. Safari) | ||
| // When the method is not available, we keep select overlay clickable not invisible | ||
| // When the method is available, hidden select is not directly clickable but opens programmatically | ||
| // The last approach looks slightly better without focusing select by click | ||
| const isShowPickerAvailable = 'showPicker' in HTMLSelectElement.prototype | ||
|
|
||
| /** | ||
| * Handle label click to open the native select picker if possible. | ||
| * | ||
| * @param event - Click event | ||
| */ | ||
| function onLabelClick(event: MouseEvent) { | ||
| if (!isShowPickerAvailable) { | ||
| return | ||
| } | ||
| event?.preventDefault() | ||
| selectElement.value!.showPicker() | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <NcFormBoxItem | ||
| tag="label" | ||
| :for="selectId" | ||
| :label | ||
| :description="selectedLabel" | ||
| :disabled | ||
| :pure="!isShowPickerAvailable" | ||
| inverted-accent | ||
| @click="onLabelClick"> | ||
| <template #icon> | ||
| <NcIconSvgWrapper :path="mdiUnfoldMoreHorizontal" inline /> | ||
| </template> | ||
| <template #extra="{ descriptionId }"> | ||
| <select | ||
| :id="selectId" | ||
| ref="select" | ||
| v-model="modelValue" | ||
| :class="[$style.hiddenSelect, { [$style.hiddenSelect_manual]: isShowPickerAvailable }]" | ||
| :aria-describedby="descriptionId"> | ||
| <option v-for="option in options" :key="option.value" :value="option.value"> | ||
| {{ option.label }} | ||
| </option> | ||
| </select> | ||
| </template> | ||
| </NcFormBoxItem> | ||
| </template> | ||
|
|
||
| <style lang="scss" module> | ||
| .hiddenSelect { | ||
| position: absolute; | ||
| inset: 0; | ||
| margin: 0; | ||
| height: auto; | ||
| cursor: pointer; | ||
| /* TODO: does it work well cross-browser? */ | ||
| opacity: 0; | ||
| } | ||
|
|
||
| // Select is open manual instead of opening by click on invisible select | ||
| .hiddenSelect_manual { | ||
| pointer-events: none; | ||
| } | ||
| </style> | ||
|
|
||
| <docs> | ||
| ### General | ||
|
|
||
| Native select wrapper to be used inside `NcFormBox`. | ||
|
|
||
| ```vue | ||
| <script> | ||
| export default { | ||
| data() { | ||
| return { | ||
| playSoundChat: 'always', | ||
| playSoundCall: 'always', | ||
| enableCallbox: 'always', | ||
| notificationLevelOptions: [ | ||
| { label: 'Always', value: 'always' }, | ||
| { label: 'Only when away', value: 'away' }, | ||
| { label: 'Never', value: 'never' }, | ||
| ], | ||
| } | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <NcFormGroup label="Notifications and sounds"> | ||
| <NcFormBox> | ||
| <NcFormBoxSelectNative v-model="playSoundChat" label="Play chat notification sound" :options="notificationLevelOptions" /> | ||
| <NcFormBoxSelectNative v-model="playSoundCall" label="Play call notification sound" :options="notificationLevelOptions" /> | ||
| <NcFormBoxSelectNative v-model="enableCallbox" label="Show call notification popup" :options="notificationLevelOptions" /> | ||
| </NcFormBox> | ||
| </NcFormGroup> | ||
| </template> | ||
| ``` | ||
| </docs> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 './NcFormBoxSelectNative.vue' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The only thing is that this makes it so that on macOS, the currently selected item's label is aligned to the top of the component, but in reality, the label is on the bottom.
Not sure if there is an easy fix for this though.
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.
Could you share a screenshot? I'm not sure I get what you mean.
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.
Your screenshot in the issue description also exhibits this issue. Basically,
selecton macOS (and to an extent iOS) moves the list up/down depending on the selected item to align the currently selected list item with the component.Essentially, the label in the list and the label in the component shouldn't move when opening the list and should be perfectly (or at least closely) aligned.
Example of how it should look, seeing "Goldfish" only once:
whereas for us, comparatively, it is shifted up and you see "Never" twice:
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.
But in the example the select is small, and our button is 40px.
If the entire button just were a select on the exact same place, it would look exactly the same.
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.
It is not shifted relative to macOS's select's picker position
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.
I think it's better if I explain it this way: since the component is taller than list items, right now the list is basically aligned as if there was a less tall invisible select that was top-aligned.
But since our label is at the bottom, it should act like there is a less tall invisible select that is bottom-aligned instead.
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.
And yes, but it should basically be as if only the bottom half were a select, since that is where our label is, not the top half. Not sure if this is easily doable though.
(Maybe if the text in the invisible select could be also bottom-aligned? But maybe it's not smart enough to pick up on that even if it is possible.)