Skip to content

Feature Request - Cross-platform "modify" for keydown filter. #654

@bennadel

Description

@bennadel

I believe there is a common pattern to modify some keydown events using a different key on Mac and Windows operating systems. The one I see most commonly is using CMD+Enter on Mac and CTRL+Enter on Windows in order to submit a form while typing in a textarea.

I know that the data-action attribute on an element can take multiple actions; so, I could, in theory, achieve this as follows:

<form data-controller="demo">
    <textarea data-action="
        keydown.meta+enter->demo#submit
        keydown.ctrl+enter->demo#submit
    "></textarea>
</form>

But, considering that I'd want to use this pattern in many places, this seems verbose.

My next thought was to register a custom keydown filter - mod - as follows:

var macOsPattern = /Mac|iPod|iPhone|iPad/;

// Map the "mod" filter pattern to the OS-specific key.
defaultSchema.keyMappings[ "mod" ] = macOsPattern.test( window.navigator.platform )
	? "meta"
	: "ctrl"
;

window.Stimulus = Application.start( document.documentElement, defaultSchema );

... with the hope being that I could use something like keydown.mod+enter where it would be evaluated as meta+enter on Mac and ctrl+enter on Windows. But, unfortunately, it looks like modifier keys are handled separately from the custom key mappings.

So, my thought would be to have Stimulus add a new modifier call mod or modify that would be the cross-platform evaluation.

Anyway, just throwing that out there. I raised it on the Hotwired community forum, and I tried Googling for it, and I didn't see anyone discussing this anywhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions