Skip to content

[checkbox] event.shiftkey always false #3875

@cgatian

Description

@cgatian

Bug report

Current behavior

The eventDetails.event does not have the correct value for the shiftKey property. It always returns false.

Expected behavior

Clicking the checkbox while the shift key is pressed should set event.shiftKey to true.

<Checkbox.Root onCheckedChange={(_checked, eventDetails) => {
          const shift = (eventDetails.event as MouseEvent)?.shiftKey ?? false;
          console.log('shift', shift); // always false
}}>

Reproducible example

Stackblitz Repro

Base UI version

v1.1.0

Which browser are you using?

Edge

Which OS are you using?

Mac OS

Additional context

I was expecting eventDetails.event to be a React synthetic event which would have nativeEvent prop but it looks like it's a PointerEvent. Perhaps there's some underlying event forwarding happening? Sorry Im not quite sure. 😕

Image

Ok yeah it looks Base UI clicks the input programatically, which most likely loses the React event details.

onClick(event: React.MouseEvent) {
if (readOnly || disabled) {
return;
}
event.preventDefault();
inputRef.current?.click();
},

Perhaps instead of calling click(), dispatch a SyntheticEvent preserving the original event's state:

onClick(event: React.MouseEvent) {
  if (readOnly || disabled) {
    return;
  }
  
  event.preventDefault();
  
  if (inputRef.current) {
    // Create a new MouseEvent with preserved keyboard state
    const syntheticEvent = new MouseEvent('click', {
      shiftKey: event.shiftKey,
      ctrlKey: event.ctrlKey,
      altKey: event.altKey,
      metaKey: event.metaKey,
    });
    
    inputRef.current.dispatchEvent(syntheticEvent);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: checkboxChanges related to the checkbox component.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions