Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Icon.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // @flow
  2. import React, { useCallback } from 'react';
  3. import { Container } from '../../react/base';
  4. import { styleTypeToObject } from '../../styles';
  5. type Props = {
  6. /**
  7. * Class name for the web platform, if any.
  8. */
  9. className?: string,
  10. /**
  11. * Color of the icon (if not provided by the style object).
  12. */
  13. color?: string,
  14. /**
  15. * Id prop (mainly for autotests).
  16. */
  17. id?: string,
  18. /**
  19. * Id of the icon container
  20. */
  21. containerId?: string,
  22. /**
  23. * Function to invoke on click.
  24. */
  25. onClick?: Function,
  26. /**
  27. * The size of the icon (if not provided by the style object).
  28. */
  29. size?: number | string,
  30. /**
  31. * The preloaded icon component to render.
  32. */
  33. src: Function,
  34. /**
  35. * Style object to be applied.
  36. */
  37. style?: Object,
  38. /**
  39. * aria disabled flag for the Icon.
  40. */
  41. ariaDisabled?: boolean,
  42. /**
  43. * aria label for the Icon.
  44. */
  45. ariaLabel?: string,
  46. /**
  47. * whether the element has a popup
  48. */
  49. ariaHasPopup?: boolean,
  50. /**
  51. * whether the element has a pressed
  52. */
  53. ariaPressed?: boolean,
  54. /**
  55. * id of description label
  56. */
  57. ariaDescribedBy?: string,
  58. /**
  59. * whether the element popup is expanded
  60. */
  61. ariaExpanded?: boolean,
  62. /**
  63. * The id of the element this button icon controls
  64. */
  65. ariaControls?: string,
  66. /**
  67. * tabIndex for the Icon.
  68. */
  69. tabIndex?: number,
  70. /**
  71. * role for the Icon.
  72. */
  73. role?: string,
  74. /**
  75. * keypress handler.
  76. */
  77. onKeyPress?: Function,
  78. /**
  79. * keydown handler.
  80. */
  81. onKeyDown?: Function
  82. }
  83. export const DEFAULT_COLOR = navigator.product === 'ReactNative' ? 'white' : undefined;
  84. export const DEFAULT_SIZE = navigator.product === 'ReactNative' ? 36 : 22;
  85. /**
  86. * Implements an Icon component that takes a loaded SVG file as prop and renders it as an icon.
  87. *
  88. * @param {Props} props - The props of the component.
  89. * @returns {Reactelement}
  90. */
  91. export default function Icon(props: Props) {
  92. const {
  93. className,
  94. color,
  95. id,
  96. containerId,
  97. onClick,
  98. size,
  99. src: IconComponent,
  100. style,
  101. ariaHasPopup,
  102. ariaLabel,
  103. ariaDisabled,
  104. ariaExpanded,
  105. ariaControls,
  106. tabIndex,
  107. ariaPressed,
  108. ariaDescribedBy,
  109. role,
  110. onKeyPress,
  111. onKeyDown,
  112. ...rest
  113. }: Props = props;
  114. const {
  115. color: styleColor,
  116. fontSize: styleSize,
  117. ...restStyle
  118. } = styleTypeToObject(style ?? {});
  119. const calculatedColor = color ?? styleColor ?? DEFAULT_COLOR;
  120. const calculatedSize = size ?? styleSize ?? DEFAULT_SIZE;
  121. const onKeyPressHandler = useCallback(e => {
  122. if ((e.key === 'Enter' || e.key === ' ') && onClick) {
  123. e.preventDefault();
  124. onClick(e);
  125. } else if (onKeyPress) {
  126. onKeyPress(e);
  127. }
  128. }, [ onClick, onKeyPress ]);
  129. const jitsiIconClassName = calculatedColor ? 'jitsi-icon' : 'jitsi-icon jitsi-icon-default';
  130. return (
  131. <Container
  132. { ...rest }
  133. aria-controls = { ariaControls }
  134. aria-describedby = { ariaDescribedBy }
  135. aria-disabled = { ariaDisabled }
  136. aria-expanded = { ariaExpanded }
  137. aria-haspopup = { ariaHasPopup }
  138. aria-label = { ariaLabel }
  139. aria-pressed = { ariaPressed }
  140. className = { `${jitsiIconClassName} ${className || ''}` }
  141. id = { containerId }
  142. onClick = { onClick }
  143. onKeyDown = { onKeyDown }
  144. onKeyPress = { onKeyPressHandler }
  145. role = { role }
  146. style = { restStyle }
  147. tabIndex = { tabIndex }>
  148. <IconComponent
  149. fill = { calculatedColor }
  150. height = { calculatedSize }
  151. id = { id }
  152. width = { calculatedSize } />
  153. </Container>
  154. );
  155. }
  156. Icon.defaultProps = {
  157. className: ''
  158. };