Skip to content
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

Blazor: Focus the first input when opening a modal #5547

Closed
hikalkan opened this issue Sep 21, 2020 · 12 comments · Fixed by #6467
Closed

Blazor: Focus the first input when opening a modal #5547

hikalkan opened this issue Sep 21, 2020 · 12 comments · Fixed by #6467

Comments

@hikalkan
Copy link
Member

For large screens, because it can be problem for mobile devices. It would be good if we can make this automatic out of the box.

@hikalkan hikalkan added this to the 4.0 milestone Sep 21, 2020
@hikalkan hikalkan changed the title Focus the first input when opening a modal Blazor: Focus the first input when opening a modal Sep 21, 2020
@hikalkan hikalkan modified the milestones: 4.0, 3.3 Sep 21, 2020
@hikalkan hikalkan modified the milestones: 4.0-preview, 4.1-preview Oct 18, 2020
@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

@hikalkan I have several ideas and I would like your opinion.

  1. Create <Autofocus> that would wrap an input. In the backend, it would detect screen size or device type and then focus the wrapped input. It would do this only the first time page is loaded

Example

<Autofocus>
  <TextEdit />
</Autofocus>
  1. The other solution would be similar in behavior, but I would use existing Form or Validations components with Autofocus parameter. This parameter would say what input needs to be focused on the first load.

Example

<Form Autofocus="@search">
  <TextEdit @ref="search" />
</Form>

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

PS. we already have a Focus() method on TextEdit that we can use to set focus programmatically. So we can use that as a first solution. But we still need to detect if it's in mobile device.

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

Modals are a little problematic since they are always loaded on the page so we cannot use page-load to focus the input. SO for modals I propose:

  1. Add Autofocus parameter, similar to 2nd approach. So whenever modal is activated through the .Show() or Visible="true" I can autofocus the input
<Modal Autofocus="@search">
    <ModalContent>      
        <ModalBody>
            <TextEdit @ref="search" />
        </ModalBody>
    </ModalContent>
</Modal>

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

@cotur and @ilkayilknur I would also like your feedback on this if possible

@hikalkan
Copy link
Member Author

Modal Autofocus="@search" and <Form Autofocus="@search"> seems fine to me.

@ilkayilknur
Copy link
Contributor

Is it possible to write something like that?

<TextEdit AutoFocus="true"/>

I also like the first declarative approach. Honestly, I wouldn't say I like giving names to the components and pass them to the other components. This makes code more complicated.

@hikalkan
Copy link
Member Author

hikalkan commented Nov 25, 2020

I suppose <TextEdit AutoFocus="true"/> is not possible. Otherwise, it would be my first choose :)

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

@ilkayilknur <TextEdit AutoFocus="true"/> is the easiest approach, but it would work for pages only. It would not work well once it's placed inside of a modal.

Modal Autofocus="@search" and <Form Autofocus="@search"> makes more sense as that way we cover most of the things.

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

🤔 Maybe when I do this <TextEdit AutoFocus="true"/> and I detect TextEdit is placed in modal, it would be automatically detected by the Modal and then focused...

@cotur
Copy link
Contributor

cotur commented Nov 25, 2020

🤔 Maybe when I do this and I detect TextEdit is placed in modal, it would be automatically detected by the Modal and then focused...

This looks like the best way.

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

I agree, and I will try and see how it goes.

@stsrki
Copy link
Contributor

stsrki commented Nov 25, 2020

I made a quick prototype and <TextEdit Autofocus="true"/> when placed in Modal is working really well. There are some limitations though.

  1. Only one component can be marked with Autofocus.
  2. I still need to figure out how to autofocus dynamically based on business rules that control which component will be autofocused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants