Source

adminjs-design-system/src/molecules/nav-group.tsx

  1. import React, { useState } from 'react'
  2. import styled from 'styled-components'
  3. import { Icon } from '../atoms/icon'
  4. import { Box } from '../atoms/box/box'
  5. import { Text } from '../atoms/text'
  6. import { cssClass } from '../utils/css-class'
  7. import themeGet from '../utils/theme-get'
  8. const NavGroupTitle = styled(Text)`
  9. padding: 11px 20px;
  10. margin: 0;
  11. color: ${themeGet('colors', 'grey100')};
  12. border-radius: 9999px;
  13. display: flex;
  14. cursor: pointer;
  15. & > ${Text} {
  16. display: block;
  17. flex-grow: 1;
  18. line-height: ${themeGet('lineHeights', 'default')};
  19. margin-bottom: 0;
  20. }
  21. & + ${Box} {
  22. padding-left: ${themeGet('space', 'xxl', '12px')};
  23. }
  24. & > .${cssClass('Icon')} svg {
  25. vertical-align: middle;
  26. padding-bottom: 2px;
  27. flex-shrink: 0;
  28. }
  29. & > .${cssClass('Icon')}:first-child {
  30. padding-right: ${themeGet('space', 'md')};
  31. }
  32. & > .${cssClass('Icon')}:last-child {
  33. padding-left: ${themeGet('space', 'sm')};
  34. }
  35. `
  36. NavGroupTitle.defaultProps = {
  37. className: cssClass('NavGroupTitle'),
  38. }
  39. /**
  40. * @memberof NavGroup
  41. * @alias NavGroupProps
  42. */
  43. export type NavGroupProps = {
  44. /** Title of the navigation group */
  45. title: string;
  46. /** Optional icon */
  47. icon?: string;
  48. }
  49. /**
  50. * @classdesc
  51. *
  52. * > This component is deprecated in favour of {@link NavigationElement} and
  53. * {@link Navigation} components
  54. *
  55. * <img src="components/navgroup.png" />
  56. *
  57. * NavGroup is used in a navigation sidebar to group similar elements
  58. *
  59. * ### Usage
  60. *
  61. * ```javascript
  62. * import { NavGroup, NavGroupProps } from '@adminjs/design-system'
  63. * ```
  64. *
  65. * @component
  66. * @subcategory Molecules
  67. * @hideconstructor
  68. * @see NavGroupProps
  69. * @see {@link https://storybook.adminjs.co/?path=/story/designsystem-molecules-navgroup--default Storybook}
  70. * @example
  71. * return (
  72. * <Box py="xl">
  73. * <NavGroup title="Some group title" icon="Add">
  74. * <Text>Some group element</Text>
  75. * </NavGroup>
  76. * </Box>
  77. * )
  78. * @section design-system
  79. * @deprecated in favour of {@link Navigation} and {@link NavigationElement} components
  80. */
  81. const NavGroup: React.FC<NavGroupProps> = (props) => {
  82. const { title, icon, children } = props
  83. const [isItOpen, toggleOpen] = useState(true)
  84. const chevron = isItOpen ? 'ChevronUp' : 'ChevronDown'
  85. return (
  86. <Box className={cssClass('NavGroup')}>
  87. <NavGroupTitle
  88. onClick={(): void => toggleOpen(!isItOpen)}
  89. bg={isItOpen ? 'grey20' : 'transparent'}
  90. >
  91. <Icon icon={icon || 'Settings'} />
  92. <Text>{title}</Text>
  93. <Icon icon={chevron} />
  94. </NavGroupTitle>
  95. {isItOpen ? (
  96. <Box pb="xl" pt="sm">
  97. {children}
  98. </Box>
  99. ) : ''}
  100. </Box>
  101. )
  102. }
  103. export { NavGroup }
  104. export default NavGroup