-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix icon + add utilitary for MUI icons
- Loading branch information
Florent Mariotti
committed
Jan 10, 2025
1 parent
668a92f
commit 3e159fd
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createElement } from "react"; | ||
|
||
import { SvgIconTypeMap } from "@mui/material"; | ||
import { OverridableComponent } from "@mui/material/OverridableComponent"; | ||
import { renderToString } from "react-dom/server"; | ||
|
||
type MuiIconComponent = OverridableComponent<SvgIconTypeMap<{}, "svg">> & { | ||
muiName: string; | ||
}; | ||
|
||
export const getMuiIconPath = (IconComponent: MuiIconComponent): string => { | ||
try { | ||
// Render the icon component to string | ||
const svgString = renderToString(createElement(IconComponent, {})); | ||
|
||
// Create a temporary DOM element to parse the SVG | ||
const div = document.createElement("div"); | ||
div.innerHTML = svgString; | ||
|
||
// Get the path element | ||
const pathElement = div.querySelector("path"); | ||
if (!pathElement) { | ||
throw new Error("No path element found in SVG"); | ||
} | ||
|
||
// Get the d attribute which contains the path data | ||
const pathData = pathElement.getAttribute("d"); | ||
if (!pathData) { | ||
throw new Error("No path data found"); | ||
} | ||
|
||
return pathData; | ||
} catch (error) { | ||
console.error("Error extracting path from MUI icon:", error); | ||
return ""; | ||
} | ||
}; |