userselect.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. export type UserSelectValues = {
  2. MozUserSelect: string | null;
  3. msUserSelect: string | null;
  4. userSelect: string | null;
  5. webkitUserSelect: string | null;
  6. };
  7. export const setBodyUserSelect = (nextValues: UserSelectValues): UserSelectValues => {
  8. // NOTE: Vendor prefixes other than `ms` should begin with a capital letter.
  9. // ref: https://reactjs.org/docs/dom-elements.html#style
  10. const previousValues = {
  11. userSelect: document.body.style.userSelect,
  12. // MozUserSelect is not typed in TS
  13. // @ts-expect-error
  14. MozUserSelect: document.body.style.MozUserSelect,
  15. // msUserSelect is not typed in TS
  16. // @ts-expect-error
  17. msUserSelect: document.body.style.msUserSelect,
  18. webkitUserSelect: document.body.style.webkitUserSelect,
  19. };
  20. document.body.style.userSelect = nextValues.userSelect || '';
  21. // MozUserSelect is not typed in TS
  22. // @ts-expect-error
  23. document.body.style.MozUserSelect = nextValues.MozUserSelect || '';
  24. // msUserSelect is not typed in TS
  25. // @ts-expect-error
  26. document.body.style.msUserSelect = nextValues.msUserSelect || '';
  27. document.body.style.webkitUserSelect = nextValues.webkitUserSelect || '';
  28. return previousValues;
  29. };