{"id":16584688,"url":"https://github.com/igoodie/react-compound-composer","last_synced_at":"2025-10-29T08:31:22.721Z","repository":{"id":186983619,"uuid":"676091990","full_name":"iGoodie/react-compound-composer","owner":"iGoodie","description":"🖇️ Spend less time crafting your complex Compound Components","archived":false,"fork":false,"pushed_at":"2024-10-05T22:52:33.000Z","size":159,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T23:51:03.553Z","etag":null,"topics":["complex","component","compound","library","nested","react"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/react-compound-composer","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-sa-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iGoodie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-08-08T12:15:23.000Z","updated_at":"2024-10-18T06:45:10.000Z","dependencies_parsed_at":"2024-03-19T21:00:14.348Z","dependency_job_id":null,"html_url":"https://github.com/iGoodie/react-compound-composer","commit_stats":null,"previous_names":["igoodie/react-compound-composer"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGoodie%2Freact-compound-composer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGoodie%2Freact-compound-composer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGoodie%2Freact-compound-composer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iGoodie%2Freact-compound-composer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iGoodie","download_url":"https://codeload.github.com/iGoodie/react-compound-composer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238795292,"owners_count":19531706,"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":["complex","component","compound","library","nested","react"],"created_at":"2024-10-11T22:45:19.194Z","updated_at":"2025-10-29T08:31:17.323Z","avatar_url":"https://github.com/iGoodie.png","language":"TypeScript","funding_links":["https://www.patreon.com/iGoodie"],"categories":[],"sub_categories":[],"readme":"\u003c!-- Logo --\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/iGoodie/react-compound-composer/master/.github/assets/logo.svg\" height=\"200px\" alt=\"Logo\"/\u003e\n\u003c/p\u003e\n\n\u003c!-- Slogan --\u003e\n\u003cp align=\"center\"\u003e\n   Spend less time crafting your Compound Components structure\n\u003c/p\u003e\n\u003c!-- Badges --\u003e\n\u003cp align=\"center\"\u003e\n\n  \u003c!-- Main Badges --\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/iGoodie/paper-editor/master/.github/assets/main-badge.svg\" height=\"20px\"/\u003e\n  \u003ca href=\"https://www.npmjs.com/package/react-compound-composer\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/react-compound-composer\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/iGoodie/react-compound-composer/tags\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/v/tag/iGoodie/react-compound-composer\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/iGoodie/paper-editor\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/languages/top/iGoodie/react-compound-composer\"/\u003e\n  \u003c/a\u003e\n\n  \u003cbr/\u003e\n\n  \u003c!-- Github Badges --\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/iGoodie/paper-editor/master/.github/assets/github-badge.svg\" height=\"20px\"/\u003e\n  \u003ca href=\"https://github.com/iGoodie/react-compound-composer/commits/master\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/last-commit/iGoodie/react-compound-composer\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/iGoodie/react-compound-composer/issues\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/issues/iGoodie/react-compound-composer\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/iGoodie/react-compound-composer/tree/master/src\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/languages/code-size/iGoodie/react-compound-composer\"/\u003e\n  \u003c/a\u003e\n\n  \u003cbr/\u003e\n\n  \u003c!-- Support Badges --\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/iGoodie/paper-editor/master/.github/assets/support-badge.svg\" height=\"20px\"/\u003e\n  \u003ca href=\"https://discord.gg/KNxxdvN\"\u003e\n    \u003cimg src=\"https://img.shields.io/discord/610497509437210624?label=discord\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.patreon.com/iGoodie\"\u003e\n    \u003cimg src=\"https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3DiGoodie%26type%3Dpatrons\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n# Description\n\nThis library aims to make it easier to develop and maintain **Compound Components** by encapsulating **context creation** and **compound composition** logic under two core helpers.\n\nCheck those amazing posts to learn more about Compound Components:\n\n- https://www.smashingmagazine.com/2021/08/compound-components-react/\n- https://betterprogramming.pub/compound-component-design-pattern-in-react-34b50e32dea0\n- https://blog.logrocket.com/understanding-react-compound-components/\n\n# Examples/Demo\n\n- [Stackblitz Collection](https://stackblitz.com/@iGoodie/collections/react-compound-composer)\n\n1. [Simple Counter Example](https://stackblitz.com/edit/stackblitz-starters-e639ls?file=src%2FCounter.component.tsx): An almost in-line example of a Counter with its own state. Here just for a very quick proof-of-concept.\n2. [A Better Structured Example](https://stackblitz.com/edit/stackblitz-starters-ltkqyc?file=src%2Fcomponents%2Faccordion%2Faccordion.tsx): An example of an Accordion compound\n3. [Nested Compounds Example](https://stackblitz.com/edit/stackblitz-starters-aexdiu?file=src%2Fcomponents%2Faccordion%2Fbody%2Faccordion-body.tsx): An example of nested compound\n4. [Flattened Root Example](https://stackblitz.com/edit/stackblitz-starters-yhr2uv?file=src%2FApp.tsx): An example of how you can flatten `Root` components\n\n# How to Use?\n\n## 1. Create your Context\n\nStart off by creating your context. This context will be available via a hook on all the sub-components. A good way to keep a dispatch-editable state across the components.\n\n```tsx\nimport { contextBuilder } from \"react-compound-composer\";\n\ninterface CounterRootProps extends React.PropsWithChildren {\n  initialCount?: number;\n}\n\nconst {\n  Consumer: CounterConsumer, // Consumer is also returned, just for convenience\n  Provider: CounterProvider,\n  useContext: useCounterContext,\n} = contextBuilder((rootProps: CounterRootProps) =\u003e {\n  const [count, setCount] = useState(rootProps.initialCount ?? 0);\n\n  return {\n    count,\n    increase: (count: number) =\u003e setCount((c) =\u003e c + count),\n    decrease: (count: number) =\u003e setCount((c) =\u003e c - count),\n  };\n});\n```\n\n## 2. Create your Root \u0026 Sub Components\n\nCreate a few components to be composed under the compound.\n\n```tsx\nimport React from \"react\";\n\nconst CounterRoot = (props: CounterRootProps) =\u003e {\n  return (\n    \u003cdiv\n      style={{\n        display: \"flex\",\n        flexDirection: \"column\",\n        gap: 10,\n        alignItems: \"center\",\n      }}\n    \u003e\n      {props.children}\n    \u003c/div\u003e\n  );\n};\n```\n\n```tsx\nimport React from \"react\";\n\nconst CounterCount = () =\u003e {\n  const ctx = useCounterContext();\n  return \u003cspan\u003e{ctx.count}\u003c/span\u003e;\n};\n```\n\n```tsx\nimport React from \"react\";\n\nconst CounterIncrease = () =\u003e {\n  const ctx = useCounterContext();\n  return \u003cbutton onClick={() =\u003e ctx.increase(1)}\u003eIncrease\u003c/button\u003e;\n};\n```\n\n```tsx\nimport React from \"react\";\n\nconst CounterDecrease = () =\u003e {\n  const ctx = useCounterContext();\n  return \u003cbutton onClick={() =\u003e ctx.decrease(1)}\u003eDecrease\u003c/button\u003e;\n};\n```\n\n## 3. Compose your Compound with them!\n\nFinally compose your Compound with the components you've created.\n\n```tsx\nimport { compoundBuilder } from \"react-compound-composer\";\n\nexport const Counter = compoundBuilder({\n  name: \"Counter\",\n  provider: CounterProvider,\n  components: {\n    Root: CounterRoot,\n    Count: CounterCount,\n    Increase: CounterIncrease,\n    Decrease: CounterDecrease,\n  },\n});\n```\n\n## 4. Enjoy your Compound!\n\nUse your compound as desired.\n\n```tsx\nexport default function App() {\n  return (\n    \u003cmain\u003e\n      \u003cCounter.Root\u003e\n        \u003cCounter.Increase /\u003e\n        \u003cCounter.Count /\u003e\n        \u003cCounter.Decrease /\u003e\n      \u003c/Counter.Root\u003e\n    \u003c/main\u003e\n  );\n}\n```\n\n## How to Flatten `Root` Components?\n\nIf you prefer using the root components without actually using their `Root` properties, you can set the `flattenRoot` option to `true`. Like so:\n\n```tsx\nimport { compoundBuilder } from \"react-compound-composer\";\n\nexport const Counter = compoundBuilder({\n  name: \"Counter\",\n  provider: CounterProvider,\n  flattenRoot: true,\n  components: {\n    Root: CounterRoot,\n    Count: CounterCount,\n    Increase: CounterIncrease,\n    Decrease: CounterDecrease,\n  },\n});\n```\n\nand use the Compound like so:\n\n```tsx\nexport default function App() {\n  return (\n    \u003cmain\u003e\n      \u003cCounter\u003e\n        \u003cCounter.Increase /\u003e\n        \u003cCounter.Count /\u003e\n        \u003cCounter.Decrease /\u003e\n      \u003c/Counter\u003e\n    \u003c/main\u003e\n  );\n}\n```\n\n## License\n\n\u0026copy; 2023 Taha Anılcan Metinyurt (iGoodie)\n\nFor any part of this work for which the license is applicable, this work is licensed under the [Attribution-ShareAlike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/) license. (See LICENSE).\n\n\u003ca rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/4.0/\"\u003e\u003cimg alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by-sa/4.0/88x31.png\" /\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figoodie%2Freact-compound-composer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figoodie%2Freact-compound-composer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figoodie%2Freact-compound-composer/lists"}