{"id":14384809,"url":"https://github.com/FezVrasta/react-element-queries","last_synced_at":"2025-08-23T18:30:56.055Z","repository":{"id":66255341,"uuid":"89946112","full_name":"FezVrasta/react-element-queries","owner":"FezVrasta","description":"🐣🐷 Conditionally render pieces of UI based on element queries.","archived":false,"fork":false,"pushed_at":"2018-03-02T11:49:16.000Z","size":44,"stargazers_count":71,"open_issues_count":1,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-08-02T10:58:12.094Z","etag":null,"topics":["element-queries","react","responsive"],"latest_commit_sha":null,"homepage":"https://fezvrasta.github.io/react-element-queries","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/FezVrasta.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}},"created_at":"2017-05-01T17:30:47.000Z","updated_at":"2023-08-15T16:05:00.000Z","dependencies_parsed_at":"2023-02-20T20:01:04.181Z","dependency_job_id":null,"html_url":"https://github.com/FezVrasta/react-element-queries","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/FezVrasta/react-element-queries","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FezVrasta%2Freact-element-queries","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FezVrasta%2Freact-element-queries/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FezVrasta%2Freact-element-queries/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FezVrasta%2Freact-element-queries/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FezVrasta","download_url":"https://codeload.github.com/FezVrasta/react-element-queries/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FezVrasta%2Freact-element-queries/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271762865,"owners_count":24816824,"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","status":"online","status_checked_at":"2025-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["element-queries","react","responsive"],"created_at":"2024-08-28T18:01:41.255Z","updated_at":"2025-08-23T18:30:55.812Z","avatar_url":"https://github.com/FezVrasta.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# react-element-queries\n\nReact component designed to let you conditionally render pieces of UI based on\na set of element (media) queries defined by you.\n\nThe best thing? It weights ~800 bytes gzipped (1.9KB minified) and its only\ndependency is [react-resize-aware](https://github.com/FezVrasta/react-resize-aware)\nwhich weights only 1.2KB gzipped!\n\n## Installation\n\n```\nyarn add react-element-queries\n# or\nnpm install --save react-element-queries\n```\n\n## Usage\n\nIt's super easy to use:\n\n```jsx\nimport { ElementQuery, Matches } from 'react-element-queries';\n\nconst App = () =\u003e\n  \u003cElementQuery queries={{ sm: { maxWidth: 200 }, lg: { minWidth: 201 } }}\u003e\n    \u003cMatches sm\u003eI get rendered only when `sm` matches! 🐣\u003c/Matches\u003e\n    \u003cMatches lg\u003eInstead this is rendered only when `lg` matches 🐷\u003c/Matches\u003e\n  \u003c/ElementQuery\u003e\n```\n\nAdditionally, you can match multiple element queries to render a piece of UI in\ndifferent conditions:\n\n```jsx\n\u003cMatches sm lg\u003e🐣 and 🐷\u003c/Matches\u003e\n```\n\nAnd if you need some more power, you can invert the behavior of a selector:\n\n```jsx\n\u003cMatches sm={false}\u003eI match only when `sm` isn't matching\u003c/Matches\u003e\n```\n\n### Need more power?\n\nYou can enhance your component to provide it a `matches` property you can\nuse to programmatically check for one or more element queries.  \nYou simply have to provide a `getRef` property assigned as `ref` callback,\nmake it render its `children` and use the `matches` property as follows:\n\n```jsx\nimport { makeElementQuery } from 'react-element-queries';\nconst App = ({ getRef, children, matches }) =\u003e\n  \u003cdiv ref={getRef} style={{ color: matches('sm') ? 'yellow' : 'pink' }}\u003e\n    \u003cMatches sm\u003e🐣\u003c/Matches\u003e\n    {children}\n  \u003c/div\u003e\n\nconst EnhancedApp = makeElementQuery(\n  App,\n  { sm: { maxWidth: 200 }, lg: { minWidth: 201 } }\n);\n```\n\n## Docs\n\n### Queries\nThe `queries` property is an object with a list of properties of your choice,\nyou can name them how you prefer, for instance, you could have `sm` or `small` or\n`verySmallMatcher`:\n\n- `{ sm: { maxWidth: 10 } }`\n- `{ small: { maxWidth: 100, minWidth: 20 } }`\n- `{ verySmallMatcher: { maxWidth: 10, minHeight: 30 } }`\n\nEach property contains an object with one or more expressions.\n\nSo far, the supported expressions are: `maxWidth`, `minWidth`, `maxHeight` and\n`minHeight`.\n\n### `\u003cMatches /\u003e` component\nOnce you have defined your element queries, you can use the `\u003cMatches /\u003e` component\nto conditionally render a piece of UI.  \nSimply add to it one or more element queries names to tell it to display its content\nwhen at least one of the matches the given expressions.\n\n### `matches` property\nWhen you enhance your component with `makeElementQuery`, your component will get\naccess to the `matches` property.  \nThis is a function you can call programmatically to perform any kind of operation\nwith the result of the element queries you provide it.\n\n```js\nmatches('sm') // true if `sm` is matching\nmatches('sm', 'lg') // true if `sm` and/or `lg` are matching\nmatches({ sm: false }) // true when `sm` isn't matching\nmatches({ sm: false }, 'lg') // true when `sm` doesn't match and `lg` does\n```\n\n### `width` and `height` properties\nSince React Element Queries is based on React Resize Aware, you have access to\nthe `width` and `height` properties once you have enhanced a component with\n`makeElementQuery`.\n\n```\nconst Example = makeElementQuery(\n  ({ getRef, children, width, height, matches }) =\u003e\n    \u003cdiv ref={getRef}\u003e{children}\u003c/div\u003e,\n  { sm: { maxWidth: 300 } }\n);\n```\n\n# Credits\n\nFull credits to the original idea to [@souporserious](https://github.com/souporserious).\n\n# License\n\nMIT License Copyright 2017+, Federico Zivolo","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFezVrasta%2Freact-element-queries","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFezVrasta%2Freact-element-queries","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFezVrasta%2Freact-element-queries/lists"}