@grafana/ui
Version:
Grafana Components Library
1 lines • 6.18 kB
Source Map (JSON)
{"version":3,"file":"VizLayout.mjs","sources":["../../../../src/components/VizLayout/VizLayout.tsx"],"sourcesContent":["import { css } from '@emotion/css';\nimport { FC, CSSProperties, ComponentType } from 'react';\nimport * as React from 'react';\nimport { useMeasure } from 'react-use';\n\nimport { GrafanaTheme2 } from '@grafana/data';\nimport { LegendPlacement } from '@grafana/schema';\n\nimport { useStyles2, useTheme2 } from '../../themes/ThemeContext';\nimport { getFocusStyles } from '../../themes/mixins';\nimport { ScrollContainer } from '../ScrollContainer/ScrollContainer';\n\n/**\n * @beta\n */\nexport interface VizLayoutProps {\n width: number;\n height: number;\n legend?: React.ReactElement<VizLayoutLegendProps> | null;\n children: (width: number, height: number) => React.ReactNode;\n}\n\n/**\n * @beta\n */\nexport interface VizLayoutComponentType extends FC<VizLayoutProps> {\n Legend: ComponentType<VizLayoutLegendProps>;\n}\n\n/**\n * @beta\n *\n * https://developers.grafana.com/ui/latest/index.html?path=/docs/plugins-vizlayout--docs\n */\nexport const VizLayout: VizLayoutComponentType = ({ width, height, legend, children }) => {\n const theme = useTheme2();\n const styles = useStyles2(getVizStyles);\n const containerStyle: CSSProperties = {\n display: 'flex',\n width: `${width}px`,\n height: `${height}px`,\n };\n const [legendRef, legendMeasure] = useMeasure<HTMLDivElement>();\n\n if (!legend) {\n return (\n <>\n <div style={containerStyle} className={styles.viz}>\n {children(width, height)}\n </div>\n </>\n );\n }\n\n let { placement, maxHeight = '35%', maxWidth = '60%' } = legend.props;\n\n if (document.body.clientWidth < theme.breakpoints.values.lg) {\n placement = 'bottom';\n }\n\n let size: VizSize | null = null;\n\n const legendStyle: CSSProperties = {};\n\n switch (placement) {\n case 'bottom':\n containerStyle.flexDirection = 'column';\n legendStyle.maxHeight = maxHeight;\n\n if (legendMeasure.height) {\n size = { width, height: height - legendMeasure.height };\n }\n break;\n case 'right':\n containerStyle.flexDirection = 'row';\n legendStyle.maxWidth = maxWidth;\n\n if (legendMeasure.width) {\n size = { width: width - legendMeasure.width, height };\n }\n\n if (legend.props.width) {\n legendStyle.width = legend.props.width;\n size = { width: width - legend.props.width, height };\n }\n break;\n }\n\n // This happens when position is switched from bottom to right\n // Then we preserve old with for one render cycle until legend is measured in it's new position\n if (size?.width === 0) {\n size.width = width;\n }\n\n if (size?.height === 0) {\n size.height = height;\n }\n\n return (\n <div style={containerStyle}>\n <div className={styles.viz}>{size && children(size.width, size.height)}</div>\n <div style={legendStyle} ref={legendRef}>\n <ScrollContainer>{legend}</ScrollContainer>\n </div>\n </div>\n );\n};\n\nexport const getVizStyles = (theme: GrafanaTheme2) => {\n return {\n viz: css({\n flexGrow: 2,\n borderRadius: theme.shape.radius.default,\n '&:focus-visible': getFocusStyles(theme),\n }),\n };\n};\ninterface VizSize {\n width: number;\n height: number;\n}\n\n/**\n * @beta\n */\nexport interface VizLayoutLegendProps {\n placement: LegendPlacement;\n children: React.ReactNode;\n maxHeight?: string;\n maxWidth?: string;\n width?: number;\n}\n\n/**\n * @beta\n */\nexport const VizLayoutLegend: FC<VizLayoutLegendProps> = ({ children }) => {\n return <>{children}</>;\n};\n\nVizLayout.Legend = VizLayoutLegend;\n"],"names":[],"mappings":";;;;;;;;AAkCO,MAAM,YAAoC,CAAC,EAAE,OAAO,MAAA,EAAQ,MAAA,EAAQ,UAAS,KAAM;AACxF,EAAA,MAAM,QAAQ,SAAA,EAAU;AACxB,EAAA,MAAM,MAAA,GAAS,WAAW,YAAY,CAAA;AACtC,EAAA,MAAM,cAAA,GAAgC;AAAA,IACpC,OAAA,EAAS,MAAA;AAAA,IACT,KAAA,EAAO,GAAG,KAAK,CAAA,EAAA,CAAA;AAAA,IACf,MAAA,EAAQ,GAAG,MAAM,CAAA,EAAA;AAAA,GACnB;AACA,EAAA,MAAM,CAAC,SAAA,EAAW,aAAa,CAAA,GAAI,UAAA,EAA2B;AAE9D,EAAA,IAAI,CAAC,MAAA,EAAQ;AACX,IAAA,uBACE,GAAA,CAAA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