UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 3.32 kB
{"version":3,"file":"get-curves.mjs","names":[],"sources":["../../../../src/components/RingProgress/get-curves/get-curves.ts"],"sourcesContent":["import type { DataAttributes, MantineColor } from '../../../core';\n\ninterface CurveData extends React.ComponentProps<'circle'>, DataAttributes {\n value: number;\n color: MantineColor;\n tooltip?: React.ReactNode;\n}\n\ninterface RootCurveData extends React.ComponentProps<'circle'>, DataAttributes {\n value?: never;\n color?: MantineColor;\n}\n\ninterface GetCurves {\n sections: CurveData[];\n size: number;\n thickness: number;\n renderRoundedLineCaps: boolean | undefined;\n rootColor?: MantineColor;\n sectionGap?: number;\n}\n\ninterface Curve {\n sum: number;\n offset: number;\n root: boolean;\n data: CurveData | RootCurveData;\n lineRoundCaps?: boolean;\n}\n\nexport function getCurves({\n size,\n thickness,\n sections,\n renderRoundedLineCaps,\n rootColor,\n sectionGap = 0,\n}: GetCurves) {\n const sum = sections.reduce((acc, current) => acc + current.value, 0);\n const accumulated = Math.PI * ((size * 0.9 - thickness * 2) / 2) * 2;\n let offset = accumulated;\n const curves: Curve[] = [];\n const curvesInOrder: Curve[] = [];\n\n // Convert gap from degrees to percentage of the circle\n const gapPercentage = (sectionGap / 360) * 100;\n\n for (let i = 0; i < sections.length; i += 1) {\n // Reduce section value by gap amount to create visual separation\n const adjustedValue = Math.max(0, sections[i].value - gapPercentage);\n curves.push({ sum, offset, data: { ...sections[i], value: adjustedValue }, root: false });\n offset -= (sections[i].value / 100) * accumulated;\n }\n\n curves.push({ sum, offset, data: { color: rootColor }, root: true });\n\n // Reorder curves to layer appropriately and selectively set caps to round\n\n curvesInOrder.push({ ...curves[curves.length - 1], lineRoundCaps: false });\n if (curves.length > 2) {\n curvesInOrder.push({ ...curves[0], lineRoundCaps: renderRoundedLineCaps });\n curvesInOrder.push({ ...curves[curves.length - 2], lineRoundCaps: renderRoundedLineCaps });\n for (let i = 1; i <= curves.length - 3; i += 1) {\n curvesInOrder.push({ ...curves[i], lineRoundCaps: false });\n }\n } else {\n curvesInOrder.push({ ...curves[0], lineRoundCaps: renderRoundedLineCaps });\n }\n\n return curvesInOrder;\n}\n"],"mappings":";;AA8BA,SAAgB,UAAU,EACxB,MACA,WACA,UACA,uBACA,WACA,aAAa,KACD;CACZ,MAAM,MAAM,SAAS,QAAQ,KAAK,YAAY,MAAM,QAAQ,OAAO,EAAE;CACrE,MAAM,cAAc,KAAK,OAAO,OAAO,KAAM,YAAY,KAAK,KAAK;CACnE,IAAI,SAAS;CACb,MAAM,SAAkB,EAAE;CAC1B,MAAM,gBAAyB,EAAE;CAGjC,MAAM,gBAAiB,aAAa,MAAO;AAE3C,MAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK,GAAG;EAE3C,MAAM,gBAAgB,KAAK,IAAI,GAAG,SAAS,GAAG,QAAQ,cAAc;AACpE,SAAO,KAAK;GAAE;GAAK;GAAQ,MAAM;IAAE,GAAG,SAAS;IAAI,OAAO;IAAe;GAAE,MAAM;GAAO,CAAC;AACzF,YAAW,SAAS,GAAG,QAAQ,MAAO;;AAGxC,QAAO,KAAK;EAAE;EAAK;EAAQ,MAAM,EAAE,OAAO,WAAW;EAAE,MAAM;EAAM,CAAC;AAIpE,eAAc,KAAK;EAAE,GAAG,OAAO,OAAO,SAAS;EAAI,eAAe;EAAO,CAAC;AAC1E,KAAI,OAAO,SAAS,GAAG;AACrB,gBAAc,KAAK;GAAE,GAAG,OAAO;GAAI,eAAe;GAAuB,CAAC;AAC1E,gBAAc,KAAK;GAAE,GAAG,OAAO,OAAO,SAAS;GAAI,eAAe;GAAuB,CAAC;AAC1F,OAAK,IAAI,IAAI,GAAG,KAAK,OAAO,SAAS,GAAG,KAAK,EAC3C,eAAc,KAAK;GAAE,GAAG,OAAO;GAAI,eAAe;GAAO,CAAC;OAG5D,eAAc,KAAK;EAAE,GAAG,OAAO;EAAI,eAAe;EAAuB,CAAC;AAG5E,QAAO"}