{"id":20056207,"url":"https://github.com/devgru/react-svg-guides","last_synced_at":"2025-03-02T09:26:41.061Z","repository":{"id":260377043,"uuid":"881127462","full_name":"devgru/react-svg-guides","owner":"devgru","description":"⋕ React hooks and components exporting rendered SVG elements' positions","archived":false,"fork":false,"pushed_at":"2025-02-23T19:03:34.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-23T20:19:08.076Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devgru.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-31T00:34:36.000Z","updated_at":"2025-02-23T19:03:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"1d4d9c00-a061-4e74-a275-dbcaab3c9f66","html_url":"https://github.com/devgru/react-svg-guides","commit_stats":null,"previous_names":["devgru/react-svg-guides"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgru%2Freact-svg-guides","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgru%2Freact-svg-guides/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgru%2Freact-svg-guides/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgru%2Freact-svg-guides/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devgru","download_url":"https://codeload.github.com/devgru/react-svg-guides/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241484244,"owners_count":19970241,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T12:52:27.986Z","updated_at":"2025-03-02T09:26:41.035Z","avatar_url":"https://github.com/devgru.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-svg-guides\n\n\u003e While HTML supports a variety of layout techniques, SVG is more limited in this regard.\n\u003e \n\u003e The library provides a set of React hooks and components to capture SVG elements' measurements and expose them via refs, enabling complex SVG-based layouts building.\n\nExplore `demos` package for a glimpse of what this library can help with.\n\n## Usage\n\n### Size measurement\n\n1. Use `useRefWithSize` hook to track element's width and height:\n\n```typescript jsx\nimport { useRefWithSize } from 'react-svg-guides';\n\nconst circleRef = useRefWithSize\u003cSVGCircleElement\u003e();\nreturn (\n  \u003csvg width={circleRef.width} height={circleRef.height}\u003e\n    \u003ccircle\n      ref={circleRef}\n      r={10}\n      cx={10}\n      cy={10}\n      fill=\"currentColor\"\n    /\u003e\n  \u003c/svg\u003e\n);\n```\n\n2. To combine `svg` and `html` elements, use `HTML` component. It renders as a `div` within a `foreignObject`, the `ref` is attached to the `div`:\n\n```typescript jsx\nimport { HTML, useRefWithSize } from 'react-svg-guides';\n\nconst divRef = useRefWithSize\u003cHTMLDivElement\u003e();\nreturn (\n  \u003csvg width={divRef.width} height={divRef.height}\u003e\n    \u003cHTML ref={divRef}\u003e\n      foreignObject inside SVG\n    \u003c/HTML\u003e\n  \u003c/svg\u003e\n);\n```\n\n### Position measurement\n\n1. Use `useRefWithBox` hook to track both element's size and position:\n\n```typescript jsx\nimport { useRefWithBox } from 'react-svg-guides';\n\nconst circleRef = useRefWithBox\u003cSVGCircleElement\u003e();\nreturn (\n  \u003csvg width={circleRef.width} height={circleRef.height}\u003e\n    \u003ccircle\n      ref={circleRef}\n      r={10}\n      cx={10}\n      cy={10}\n      fill=\"currentColor\"\n    /\u003e\n  \u003c/svg\u003e\n);\n```\n\n2. Optionally use `SVG` component to switch the origin point for boxes, from the page's origin to the `SVG` element's origin:\n\n```typescript jsx\nimport { SVG, HTML, useRefWithBox } from 'react-svg-guides';\n\nconst divRef = useRefWithBox\u003cHTMLDivElement\u003e();\n\nreturn (\n  \u003cSVG\u003e\n    \u003cHTML ref={divRef}\u003e\n      foreignObject inside SVG\n    \u003c/HTML\u003e\n  \u003c/SVG\u003e\n);\n```\n\n## Types\n\n```typescript\ntype RefObjectWithSize\u003cE\u003e = RefObject\u003cE | null\u003e \u0026 {\n  width: number;\n  height: number;\n};\n\ntype RefObjectWithBox\u003cE\u003e = RefObjectWithSize\u003cE\u003e \u0026 {\n  left: number;\n  horizontalCenter: number;\n  right: number;\n  top: number;\n  verticalCenter: number;\n  bottom: number;\n};\n\ninterface HtmlProps extends SVGAttributes\u003cSVGForeignObjectElement\u003e {\n  ref?: RefObject\u003cHTMLDivElement | null\u003e;\n}\n\ninterface SvgProps extends SVGAttributes\u003cSVGSVGElement\u003e {\n  ref: RefObject\u003cSVGSVGElement | null\u003e;\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevgru%2Freact-svg-guides","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevgru%2Freact-svg-guides","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevgru%2Freact-svg-guides/lists"}