{"id":4335,"url":"https://github.com/alexfoxy/react-native-units","last_synced_at":"2025-07-05T20:04:49.198Z","repository":{"id":57341116,"uuid":"177818783","full_name":"alexfoxy/react-native-units","owner":"alexfoxy","description":"A collection of useful units and a simple grid implementation for responsive layouts in React Native.","archived":false,"fork":false,"pushed_at":"2019-04-10T11:28:22.000Z","size":6,"stargazers_count":29,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-19T19:35:03.993Z","etag":null,"topics":["css","javascript","pixels","react-native","responsive","units"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/alexfoxy.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}},"created_at":"2019-03-26T15:38:27.000Z","updated_at":"2025-03-05T05:22:32.000Z","dependencies_parsed_at":"2022-09-12T23:42:05.596Z","dependency_job_id":null,"html_url":"https://github.com/alexfoxy/react-native-units","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexfoxy/react-native-units","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfoxy%2Freact-native-units","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfoxy%2Freact-native-units/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfoxy%2Freact-native-units/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfoxy%2Freact-native-units/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexfoxy","download_url":"https://codeload.github.com/alexfoxy/react-native-units/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexfoxy%2Freact-native-units/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261726896,"owners_count":23200739,"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":["css","javascript","pixels","react-native","responsive","units"],"created_at":"2024-01-05T20:17:08.636Z","updated_at":"2025-07-05T20:04:49.182Z","avatar_url":"https://github.com/alexfoxy.png","language":"JavaScript","funding_links":[],"categories":["Components"],"sub_categories":["UI"],"readme":"\n\n# react-native-units\n\nA collection of useful units and a simple grid implementation for responsive layouts in React Native.\n\nReact Native uses *density-independent pixels*, or *dp*, as it's default unit. This will size elements so that they are roughly the same physical size on different devices. Whilst this is useful, I missed some of the units you have available in CSS. \n\n## Setup\nAdd the package from NPM. No `react-native-link` required.\n```\nyarn add react-native-units\n- or - \nnpm install react-native-units\n```\n\nImport the library where you need it:\n```javascript\nimport RNU from 'react-native-units'\n```\n\n\n## Units\n\n### vw(x=1)\n% of the screen width, e.g. `RNU.vw(10)` is equal to `10%` of the screens width\n\n### vh(x=1)\n% of the screen height, e.g. `RNU.vh(10)` is equal to `10%` of the screen height\n\n### px(x=1)\nPhysical pixels based on device pixel ratio, e.g. `RNU.px(1)` is equal to `1 pixel` on the device, handy for very thin lines!\n\n### su(x=1)\nScaled unit, similar to `rem` in CSS. You can set the scale using `RNU.setScale(scale)`. This is useful for scaling fonts and layouts depending on the device e.g.\n\n```javascript\nif(iPad) RNU.setScale(0.75) // RNU.su(10) \u003e 7.5dp\nif(iPhone5) RNU.setScale(1.5) // RNU.su(10) \u003e 15dp\n```\n\n## Grid\n\nA simple way to create grids. First set your parameters:\n\n```javascript\nRNU.setGrid({\n\tcols: 24, \n\tpadding: 20, \n\tspacing: 10 \n})\n```\nThen use the `gr, gs \u0026 gp` units to create your layout. I have made a snack [here](https://snack.expo.io/@alexfoxy/react-native-units-grid-example) which will create the example below.\n![enter image description here](https://i.imgur.com/FbC45qM.png)\n\n### gr(x=1)\nThis unit is equal to one column's width, however it will also include any spacing it encompasses. In this example, if `RNU.gr(1)` is equal to `12dp` then `RNU.gr(2)` will equal `34dp ((12*2)+10)` \n\n### gs(x=1)\nThe grid spacing. In this example `RNU.gs()` is equal to `10dp`\n\n### gp(x=1)\nThe grid padding. In this example `RNU.gp()` is equal to `20dp`\n\n\n## Screen Rotation\nAs this library depends on the screen width and height to calculate units, when the screen rotates you need to call `RNU.update()`. The easiest way is to add an `onLayout` to your main app component e.g.\n```javascript\n\u003cView onLayout={() =\u003e { RNU.update() }\u003e\n\t...\n\u003c/View\u003e\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexfoxy%2Freact-native-units","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexfoxy%2Freact-native-units","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexfoxy%2Freact-native-units/lists"}