您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.web.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* eslint-disable @typescript-eslint/naming-convention */
  2. import { Theme, adaptV4Theme, createTheme } from '@mui/material/styles';
  3. import { ITypography, IPalette as Palette1 } from '../ui/types';
  4. import { createColorTokens, createTypographyTokens } from './utils';
  5. declare module '@mui/material/styles' {
  6. // eslint-disable-next-line @typescript-eslint/no-empty-interface
  7. interface Palette extends Palette1 {}
  8. // eslint-disable-next-line @typescript-eslint/no-empty-interface
  9. interface TypographyVariants extends ITypography {}
  10. }
  11. interface ThemeProps {
  12. breakpoints: Object;
  13. colorMap: Object;
  14. font: Object;
  15. shape: Object;
  16. spacing: Array<number>;
  17. typography: Object;
  18. }
  19. /**
  20. * Creates a MUI theme based on local UI tokens.
  21. *
  22. * @param {Object} arg - The ui tokens.
  23. * @returns {Object}
  24. */
  25. export function createWebTheme({ font, colorMap, shape, spacing, typography, breakpoints }: ThemeProps) {
  26. return createTheme(adaptV4Theme({
  27. spacing,
  28. palette: createColorTokens(colorMap),
  29. shape,
  30. typography: {
  31. // @ts-ignore
  32. font,
  33. ...createTypographyTokens(typography)
  34. },
  35. breakpoints
  36. }));
  37. }
  38. /**
  39. * Find the first styled ancestor component of an element.
  40. *
  41. * @param {HTMLElement|null} target - Element to look up.
  42. * @param {string} cssClass - Styled component reference.
  43. * @returns {HTMLElement|null} Ancestor.
  44. */
  45. export const findAncestorByClass = (target: HTMLElement | null, cssClass: string): HTMLElement | null => {
  46. if (!target || target.classList.contains(cssClass)) {
  47. return target;
  48. }
  49. return findAncestorByClass(target.parentElement, cssClass);
  50. };
  51. /**
  52. * Checks if the passed element is visible in the viewport.
  53. *
  54. * @param {Element} element - The element.
  55. * @returns {boolean}
  56. */
  57. export function isElementInTheViewport(element?: Element): boolean {
  58. if (!element) {
  59. return false;
  60. }
  61. if (!document.body.contains(element)) {
  62. return false;
  63. }
  64. const { innerHeight, innerWidth } = window;
  65. const { bottom, left, right, top } = element.getBoundingClientRect();
  66. if (bottom <= innerHeight && top >= 0 && left >= 0 && right <= innerWidth) {
  67. return true;
  68. }
  69. return false;
  70. }
  71. const enterKeyElements = [ 'select', 'textarea', 'summary', 'a' ];
  72. /**
  73. * Informs whether or not the given element does something on its own when pressing the Enter key.
  74. *
  75. * This is useful to correctly submit custom made "forms" that are not using the native form element,
  76. * only when the user is not using an element that needs the enter key to work.
  77. * Note the implementation is incomplete and should be updated as needed if more complex use cases arise
  78. * (for example, the Tabs aria pattern is not handled).
  79. *
  80. * @param {Element} element - The element.
  81. * @returns {boolean}
  82. */
  83. export function operatesWithEnterKey(element: Element): boolean {
  84. if (enterKeyElements.includes(element.tagName.toLowerCase())) {
  85. return true;
  86. }
  87. if (element.tagName.toLowerCase() === 'button' && element.getAttribute('role') === 'button') {
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * Returns a common spacing from the bottom of the page for floating elements over the video space.
  94. *
  95. * @param {Theme} theme - The current theme.
  96. * @param {boolean} isToolbarVisible - Whether the toolbar is visible or not.
  97. * @returns {number}
  98. */
  99. export function getVideospaceFloatingElementsBottomSpacing(theme: Theme, isToolbarVisible: boolean) {
  100. return parseInt(isToolbarVisible ? theme.spacing(12) : theme.spacing(6), 10);
  101. }