Deselect current text selection

Q: How can I deselect the text programmatically?

A: There’s currently no specific API for deselecting text. However, as a workaround, you can quickly change the current interaction mode and set them immediately back to the previous one. That will clear the text selection.

Copy
1
2
3
4
5
6
7
function deselectText() {
  const interactionMode = instance.viewState.interactionMode;
  instance.setViewState(v =>
    v.set("interactionMode", PSPDFKit.InteractionMode.PAN)
  );
  instance.setViewState(v => v.set("interactionMode", interactionMode));
}

PSPDFKit.InteractionMode.PAN is a good option since it doesn’t trigger any change on the UI. Unlike using other interaction modes as the transition one, this won’t result in any flash of quickly disappearing content.

This has been tested with PSPDFKit for Web 2020.4.2.