{"id":28945340,"url":"https://github.com/apostrophecms/postcss-viewport-to-container-toggle","last_synced_at":"2025-10-24T22:09:22.355Z","repository":{"id":277900043,"uuid":"880157870","full_name":"apostrophecms/postcss-viewport-to-container-toggle","owner":"apostrophecms","description":null,"archived":false,"fork":false,"pushed_at":"2025-08-06T16:19:57.000Z","size":66,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-22T18:48:39.390Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/apostrophecms.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-10-29T08:18:34.000Z","updated_at":"2025-08-06T16:19:51.000Z","dependencies_parsed_at":"2025-02-16T21:54:23.987Z","dependency_job_id":"37afe644-21f5-4720-baa1-1093780da94d","html_url":"https://github.com/apostrophecms/postcss-viewport-to-container-toggle","commit_stats":null,"previous_names":["apostrophecms/postcss-viewport-to-container-toggle"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/apostrophecms/postcss-viewport-to-container-toggle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fpostcss-viewport-to-container-toggle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fpostcss-viewport-to-container-toggle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fpostcss-viewport-to-container-toggle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fpostcss-viewport-to-container-toggle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apostrophecms","download_url":"https://codeload.github.com/apostrophecms/postcss-viewport-to-container-toggle/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apostrophecms%2Fpostcss-viewport-to-container-toggle/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280872742,"owners_count":26405766,"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-10-24T02:00:06.418Z","response_time":73,"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":[],"created_at":"2025-06-23T07:12:44.474Z","updated_at":"2025-10-24T22:09:22.349Z","avatar_url":"https://github.com/apostrophecms.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# postcss-viewport-to-container-toggle \n\nA plugin for [PostCSS](https://github.com/postcss/postcss) that allows to toggle between viewport and container units based on the presence of a container data attribute.\n\n## Why?\n\nThis plugin has been originally developed to allow mobile preview without using any `iframe` in order to be as close as possible to the real rendering.\n\nFor examples, let's say we have a block in our page that is taking `50vw`. \nWe want in the case of the mobile preview (where body is a container), this block to take `50cqw` instead of `50vw`.\n\nHere is what it looks like, in normal mode, our block takes `50vw` as it did before we the initial code:\n\n![image](./images/normal.png)\n\nIn mobile preview, body being a container, this block will take `50cqw` of the container:\n\n![image](./images/mobile.png)\n\n\n## Demo\n\nThis css:\n\n```css\n.hello {\n    width: 100vw;\n    height: 100vh;\n}\n```\n\nIf you set the `modifierAttr` to `data-breakpoint-preview-mode` and `containerEl` to `body` (default), it'll be converted this way:\n\n```css\n.hello {\n    width: 100vw;\n    height: 100vh;\n}\n\n:where(body[data-breakpoint-preview-mode]) .hello {\n    width: 100cqw;\n    height: 100cqh;\n}\n```\n\nThe purpose being here to keep the existing behavior but to make the code work compatible for containers when body is in container mode.\n\nHere is another examples with media queries:\n\n```css\n@media only screen and (width \u003e 600px) and (max-width: 1000px) {\n  .hello {\n    top: 0;\n    width: 100vw;\n    height: calc(100vh - 50px);\n  }\n  .goodbye {\n    width: 100%;\n    color: #fff;\n    transform: translateX(20vw);\n  }\n}\n\n.toto {\n  width: 100vh;\n  color: white;\n}\n```\n\n\nwill become:\n\n```css\n@media only screen and (width \u003e 600px) and (max-width: 1000px) {\n  :where(body:not([data-breakpoint-preview-mode])) .hello {\n    top: 0;\n    width: 100vw;\n    height: calc(100vh - 50px);\n  }\n  :where(body:not([data-breakpoint-preview-mode])) .goodbye {\n    width: 100%;\n    color: #fff;\n    transform: translateX(20vw);\n  }\n}\n\n@container (width \u003e 600px) and (max-width: 1000px) {\n  .hello {\n    top: 0;\n    width: 100cqw;\n    height: calc(100cqh - 50px);\n  }\n  .goodbye {\n    width: 100%;\n    color: #fff;\n    transform: translateX(20cqw);\n  }\n}\n\n.toto {\n  width: 100vh;\n  color: white;\n}\n\n:where(body[data-breakpoint-preview-mode]) .toto {\n  width: 100cqh;\n}\n```\n\nAs you can see, if body has no specific attribute, the behavior stays the same. \nWhen adding `data-breakpoint-preview-mode`, data in media queries are converted to container units and moved to container queries.\n\n## Installation\n\n```bash\nnpm install postcss-viewport-to-container-toggle\n```\n\n## Getting started\n\n### Webpack\n\n```javascript\nconst postcssViewportToContainerToggle = require('postcss-viewport-to-container-toggle');\n\n{\n  loader: 'postcss-loader',\n  options: {\n    sourceMap: true,\n    postcssOptions: {\n      plugins: [\n        [\n          postcssViewportToContainerToggle({\n            modifierAttr: 'data-breakpoint-preview-mode',\n            containerEl: 'body',\n            debug: false\n          }),\n          'autoprefixer'\n        ]\n      ]\n    }\n  }\n}\n```\n\n### Vite\n\n```javascript\nconst postcssViewportToContainerToggle = require('postcss-viewport-to-container-toggle');\n\n{\n    css: {\n      postcss: {\n        plugins: [\n          postcssViewportToContainerToggle({\n            modifierAttr: 'data-breakpoint-preview-mode',\n            containerEl: 'body',\n            debug: false\n          })\n        ]\n      }\n    }\n}\n```\n\n### Options\n\n* `modifierAttr`: The attribute that will be used to toggle between viewport and container units.\n* `containerEl`: The element that will be used as container. Default: `body`\n* `debug`: If set to `true`, will output debug information. Default: `false`\n* `transform`: A function that will be called for each media query, allowing to modify its params when creating the `container`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Fpostcss-viewport-to-container-toggle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapostrophecms%2Fpostcss-viewport-to-container-toggle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapostrophecms%2Fpostcss-viewport-to-container-toggle/lists"}