{"id":13394128,"url":"https://github.com/diegohaz/singel","last_synced_at":"2025-04-05T04:15:12.167Z","repository":{"id":32544266,"uuid":"135649535","full_name":"diegohaz/singel","owner":"diegohaz","description":"Single Element Pattern","archived":false,"fork":false,"pushed_at":"2022-12-08T18:54:12.000Z","size":720,"stargazers_count":408,"open_issues_count":24,"forks_count":20,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-26T02:34:07.296Z","etag":null,"topics":["cli","linter","react","react-patterns","reactjs"],"latest_commit_sha":null,"homepage":"https://medium.freecodecamp.org/introducing-the-single-element-pattern-dfbd2c295c5d","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diegohaz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-01T00:45:45.000Z","updated_at":"2024-06-09T05:45:16.000Z","dependencies_parsed_at":"2023-01-14T21:33:04.320Z","dependency_job_id":null,"html_url":"https://github.com/diegohaz/singel","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fsingel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fsingel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fsingel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegohaz%2Fsingel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diegohaz","download_url":"https://codeload.github.com/diegohaz/singel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247092377,"owners_count":20882218,"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":["cli","linter","react","react-patterns","reactjs"],"created_at":"2024-07-30T17:01:09.908Z","updated_at":"2025-04-05T04:15:12.137Z","avatar_url":"https://github.com/diegohaz.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# \u003cimg src=\"logo/logo.png\" width=\"200\" alt=\"singel\" /\u003e\n\n[![Generated with nod](https://img.shields.io/badge/generator-nod-2196F3.svg?style=flat-square)](https://github.com/diegohaz/nod)\n[![NPM version](https://img.shields.io/npm/v/singel.svg?style=flat-square)](https://npmjs.org/package/singel)\n[![Build Status](https://img.shields.io/travis/diegohaz/singel/master.svg?style=flat-square)](https://travis-ci.org/diegohaz/singel) [![Coverage Status](https://img.shields.io/codecov/c/github/diegohaz/singel/master.svg?style=flat-square)](https://codecov.io/gh/diegohaz/singel/branch/master)\n\n**Single Element Pattern** (Singel) is a set of rules/best practices to create consistent, reliable and maintainable components in React and other component-based libraries. This is based on the idea that the **building blocks** of an application should resemble as most as possible native HTML elements. [**Read full article**](https://medium.freecodecamp.org/introducing-the-single-element-pattern-dfbd2c295c5d)\n\nThis repo is a CLI tool for checking whether React components conform to the Singel pattern.\n\n\u003cbr\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/3068563/41152955-aade0680-6aeb-11e8-9b19-819f28f2f9c2.png\" alt=\"Example\" /\u003e\n\u003cbr\u003e\u003cbr\u003e\n\n## Installation\n\n```sh\n$ npm i -g singel\n```\n\n## Usage\n\n```sh\n$ singel path/to/**/Component.js --ignore \"path/to/**/ignored/Component.js\"\n```\n\n## Projects applying Singel\n\n\u003e Feel free to send a PR adding your open source project\n\n- [Reakit](https://github.com/diegohaz/reakit)\n\n## Rules\n\n### Render only one element\n\n```jsx\n// bad - 2 elements\nconst Element = props =\u003e (\n  \u003cdiv {...props}\u003e\n    \u003cspan /\u003e\n  \u003c/div\u003e\n);\n\n// good\nconst Element = props =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// good - if Element is good\nconst Element2 = props =\u003e (\n  \u003cElement {...props} /\u003e\n);\n```\n\n### Never break the app\n\n```jsx\n// good\nconst Element = props =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - will break if getId wasn't provided\nconst Element = ({ getId, ...props }) =\u003e (\n  \u003cdiv id={getId()} {...props} /\u003e\n);\n\n// bad - will break if foo wasn't provided\nconst Element = ({ foo, ...props }) =\u003e (\n  \u003cdiv id={foo.bar} {...props} /\u003e\n);\n```\n\n### Render all HTML attributes passed as props\n\n```jsx\n// good\nconst Element = props =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - not rendering id\nconst Element = ({ id, ...props }) =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// good\nconst Element = ({ id, ...props }) =\u003e (\n  \u003cdiv id={id} {...props} /\u003e\n);\n```\n\n### Always merge the styles passed as props\n\n```jsx\n// good\nconst Element = props =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - not rendering className\nconst Element = ({ className, ...props }) =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - not rendering style\nconst Element = ({ style, ...props }) =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - replacing className instead of appending\nconst Element = props =\u003e (\n  \u003cdiv className=\"foo\" {...props} /\u003e\n);\n\n// bad - replacing style instead of merging\nconst Element = props =\u003e (\n  \u003cdiv style={{ padding: 0 }} {...props} /\u003e\n);\n\n// good\nconst Element = ({ className, ...props }) =\u003e (\n  \u003cdiv className={`foo ${className}`} {...props} /\u003e\n);\n\n// good\nconst Element = ({ style, ...props }) =\u003e (\n  \u003cdiv style={{ padding: 0, ...style }} {...props} /\u003e\n);\n```\n\n### Add all the event handlers passed as props\n\n```jsx\n// good\nconst Element = props =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - not passing onClick\nconst Element = ({ onClick, ...props }) =\u003e (\n  \u003cdiv {...props} /\u003e\n);\n\n// bad - replacing onClick prop\nconst Element = props =\u003e (\n  \u003cdiv {...props} onClick={myFunction} /\u003e\n);\n\n// good\nconst Element = ({ onClick, ...props }) =\u003e (\n  \u003cdiv onClick={onClick} {...props} /\u003e\n);\n\n// good - it's ok to replace internal event handlers\nconst Element = props =\u003e (\n  \u003cdiv onClick={myFunction} {...props} /\u003e\n);\n\n// good - calling internal and prop\nconst callAll = (...fns) =\u003e (...args) =\u003e \n  fns.forEach(fn =\u003e fn \u0026\u0026 fn(...args));\n\nconst Element = ({ onClick, ...props }) =\u003e (\n  \u003cdiv onClick={callAll(myFunction, onClick)} {...props} /\u003e\n);\n```\n\n## FAQ\n\n### How to handle nested elements?\n\nSay you have a `Button` element and you want to display a `Tooltip` when it's hovered. The first rule you'll want to break is rendering only one element. To handle that you have some options:\n\n- Use CSS pseudo-elements (such as `:after` and `:before`);\n- Create a non-singel element, which is fine;\n- Nest styles instead of components.\n\nHere's an example of how you can accomplish tha latter one:\n\n```css\n/* could also be CSS-in-JS */\n.button {\n  position: relative;\n  /* more button css */\n}\n\n.button:hover .tooltip {\n  display: block;\n}\n\n.button .tooltip {\n  display: none;\n  position: absolute;\n  /* more tooltip css */\n}\n```\n\n```jsx\nconst Button = ({ className, ...props }) =\u003e (\n  \u003cbutton className={`button ${className}`} {...props} /\u003e\n);\n\nButton.Tooltip = ({ className, ...props }) =\u003e (\n  \u003cdiv className={`tooltip ${className}`} {...props} /\u003e\n);\n```\n\nUsage:\n\n```jsx\n\u003cButton className=\"my-specific-button\"\u003e\n  \u003cButton.Tooltip className=\"my-specific-tooltip\"\u003e\n    😁\n  \u003c/Button.Tooltip\u003e\n  Hover me\n\u003c/Button\u003e\n```\n\nBoth `Button` and `Button.Tooltip` are single elements. You have all the benefits you would have by nesting them, but now with complete control over `Button.Tooltip` from outside.\n\n## License\n\nMIT © [Diego Haz](https://github.com/diegohaz)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiegohaz%2Fsingel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiegohaz%2Fsingel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiegohaz%2Fsingel/lists"}