{"id":14968145,"url":"https://github.com/hiro0218/postcss-media-hover-any-hover","last_synced_at":"2025-10-03T22:25:28.256Z","repository":{"id":237505854,"uuid":"794612895","full_name":"hiro0218/postcss-media-hover-any-hover","owner":"hiro0218","description":"PostCSS plugin wraps selectors with :hover with at-media (hover: hover) block","archived":false,"fork":false,"pushed_at":"2024-12-09T13:51:19.000Z","size":63,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T03:03:57.991Z","etag":null,"topics":["postcss","postcss-plugin"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/postcss-media-hover-any-hover","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/hiro0218.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,"publiccode":null,"codemeta":null},"funding":{"github":"hiro0218","buy_me_a_coffee":"hiro0218"}},"created_at":"2024-05-01T15:29:41.000Z","updated_at":"2024-12-09T13:51:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"0c0428d6-fd6e-49f4-9e5a-dce534292f94","html_url":"https://github.com/hiro0218/postcss-media-hover-any-hover","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"9135fe04a077bcf5bbf90d253e6a10938e79eae4"},"previous_names":["hiro0218/postcss-media-hover-any-hover"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiro0218%2Fpostcss-media-hover-any-hover","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiro0218%2Fpostcss-media-hover-any-hover/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiro0218%2Fpostcss-media-hover-any-hover/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hiro0218%2Fpostcss-media-hover-any-hover/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hiro0218","download_url":"https://codeload.github.com/hiro0218/postcss-media-hover-any-hover/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238229940,"owners_count":19437723,"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":["postcss","postcss-plugin"],"created_at":"2024-09-24T13:39:23.167Z","updated_at":"2025-10-03T22:25:28.250Z","avatar_url":"https://github.com/hiro0218.png","language":"JavaScript","funding_links":["https://github.com/sponsors/hiro0218","https://buymeacoffee.com/hiro0218"],"categories":[],"sub_categories":[],"readme":"# postcss-media-hover-any-hover\n\n[![npm version](https://img.shields.io/npm/v/postcss-media-hover-any-hover.svg?style=flat-square)](https://www.npmjs.com/package/postcss-media-hover-any-hover)\n[![npm downloads](https://img.shields.io/npm/dm/postcss-media-hover-any-hover.svg?style=flat-square)](https://www.npmjs.com/package/postcss-media-hover-any-hover)\n\npostcss-media-hover-any-hover is a PostCSS plugin that automatically wraps CSS `:hover` selectors with `@media (any-hover: hover)` blocks.\nThis prevents unintended hover effects (the so-called \"sticky hover\" problem) on touch and hybrid devices, ensuring that hover styles are only applied on devices where a pointing device (like a mouse) is available.\n\n- Desktop (with mouse): Hover effects are enabled\n- Smartphones/Tablets: Hover effects are disabled\n- Hybrid devices (e.g., Surface): Hover effects are enabled only when a mouse is connected\n\nBy using this plugin, you can achieve optimal hover behavior for each device type without sacrificing CSS maintainability.\n\n## Install\n\n```bash\nnpm i -D postcss-media-hover-any-hover\n```\n\n### PostCSS Config\n\n`postcss.config.js`:\n\n```js\nmodule.exports = {\n  plugins: [require('postcss-media-hover-any-hover')()],\n};\n```\n\n### JavaScript API\n\n```js\nconst fs = require('fs');\nconst postcss = require('postcss');\nconst postcssMediaHoverAnyHover = require('postcss-media-hover-any-hover');\n\nconst css = fs.readFileSync('input.css', 'utf8');\n\nconst output = postcss().use(postcssMediaHoverAnyHover()).process(css).css;\n```\n\n## Usage\n\nThis plugin automatically wraps rules containing `:hover` selectors with `@media (any-hover: hover)`.\nThis ensures hover effects only apply on devices that support hover functionality.\n\n### Basic Example\n\n```css\n/* Input */\na {\n  \u0026:hover {\n    text-decoration: underline;\n  }\n}\n\n/* Output */\na {\n  @media (any-hover: hover) {\n    \u0026:hover {\n      text-decoration: underline;\n    }\n  }\n}\n```\n\n### Multiple Selectors Example\n\n```css\n/* Input */\na:hover,\nbutton:hover {\n  color: red;\n}\n\n/* Output */\n@media (any-hover: hover) {\n  a:hover,\n  button:hover {\n    color: red;\n  }\n}\n```\n\n### Mixed Selectors Example\n\n```css\n/* Input */\na {\n  color: blue;\n  \u0026:hover {\n    color: red;\n  }\n}\n\n/* Output */\na {\n  color: blue;\n  @media (any-hover: hover) {\n    \u0026:hover {\n      color: red;\n    }\n  }\n}\n```\n\n### Nested Selectors Example\n\n```css\n/* Input */\n.container {\n  .button:hover {\n    color: blue;\n  }\n}\n\n/* Output */\n.container {\n  @media (any-hover: hover) {\n    .button:hover {\n      color: blue;\n    }\n  }\n}\n```\n\n### Combining with Existing Media Queries\n\n```css\n/* Input */\n@media (min-width: 768px) {\n  a:hover {\n    color: purple;\n  }\n}\n\n/* Output */\n@media (min-width: 768px) {\n  @media (any-hover: hover) {\n    a:hover {\n      color: purple;\n    }\n  }\n}\n```\n\n### Plugin Options\n\nThis plugin accepts the following options:\n\n```js\npostcssMediaHoverAnyHover({\n  // 'any-hover' or 'hover', default: 'any-hover'\n  mediaFeature: 'any-hover',\n\n  // Whether to transform :hover within existing media queries, default: false\n  transformNestedMedia: false,\n\n  // Exclude specific selector patterns, default: []\n  excludeSelectors: ['.no-transform:hover'],\n});\n```\n\n#### `mediaFeature` Option\n\nChoose between `'any-hover'` (default) and `'hover'`:\n\n- `'any-hover'`: Uses `@media (any-hover: hover)` which checks if any input device supports hover\n- `'hover'`: Uses `@media (hover: hover)` which only checks if the primary input device supports hover\n\n```js\n// Using hover instead of any-hover\npostcss().use(postcssMediaHoverAnyHover({ mediaFeature: 'hover' }));\n```\n\n#### `transformNestedMedia` Option\n\nControls whether to transform `:hover` selectors that are already inside a media query:\n\n```js\n// Transform :hover even when already inside media queries\npostcss().use(postcssMediaHoverAnyHover({ transformNestedMedia: true }));\n```\n\n#### `excludeSelectors` Option\n\nSpecify selectors that should not be transformed:\n\n```js\n// Exclude specific selectors from transformation\npostcss().use(\n  postcssMediaHoverAnyHover({\n    excludeSelectors: ['.no-transform:hover', '.special-button:hover'],\n  }),\n);\n```\n\n## Performance\n\nThis plugin is optimized to work fast and efficiently:\n\n- Early returns to avoid unnecessary processing\n- Optimized regular expression patterns\n- Data structures for fast lookups\n- Memory usage optimization\n\nThe plugin performs well even with large CSS files. To run performance tests:\n\n```bash\nnpm run perf\n```\n\n### Performance Test Results Example\n\n```\n# Small CSS (145 characters)\nAverage execution time: 0.15ms/iteration\n\n# Medium CSS (about 20k characters)\nAverage execution time: 1.81ms/iteration\n\n# Large CSS (about 170k characters)\nAverage execution time: 19.99ms/iteration\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiro0218%2Fpostcss-media-hover-any-hover","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhiro0218%2Fpostcss-media-hover-any-hover","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhiro0218%2Fpostcss-media-hover-any-hover/lists"}