Skip to content

@winner-fed/plugin-viewport

Viewport plugin for WinJS.

npm versionlicensedownloads

Built on top of postcss-mobile-forever.

Usage

Install:

bash
npm add @winner-fed/plugin-viewport -D

Add plugin to your .winrc.ts:

ts
// .winrc.ts
export default {
  plugins: ['@winner-fed/plugin-viewport'],
  // Enable configuration
  // Reference: https://github.com/wswmsword/postcss-mobile-forever?tab=readme-ov-file#%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0 
  viewport: {}
};

Options

NameTypeDefaultDesc
viewportWidthnumber|(file: string) => number750The application is developed based on this width, and the converted scalable view will be proportionally scaled based on this width view. You can pass a function to dynamically generate width, for example file => file.includes("vant") ? 375 : 750 means using 375px width for files with "vant" in the name, while other files use 750px width
mobileUnitstring"vw"Mobile portrait viewport view, what scalable unit to convert to? Setting to rem activates rem-mode
maxDisplayWidthnumber/Maximum width of the scalable view
basicRemWidthnumber/Base width for rem-mode, if not set, will be obtained from viewportWidth
enableMediaQuerybooleanfalseEnable media query mode, when enabled will automatically disable maxDisplayWidth and activate mq-mode
desktopWidthnumber600View width displayed when adapting to desktop width
landscapeWidthnumber425View width displayed when adapting to mobile landscape width
appSelectorstring/Outermost page selector, for example "#app", used to set center styles for desktop and mobile landscape, style file must contain at least an empty selector #app {}
appContainingBlock"calc"|"manual"|"auto""calc"This property is related to correcting fixed positioned elements. manual will not correct; calc will correct element dimensions through plugin active calculation, which is the default behavior; auto will force set the root containing block to appSelector via transform: translateZ(0), thereby automatically correcting elements, and requires setting the necessarySelectorWhenAuto property
necessarySelectorWhenAutostring/When appContainingBlock is set to auto, this property needs to be specified. This property specifies the element selector one level inside appSelector. See an experiment about specifying elements as containing blocks to understand how to use this property. You can also check a sample project using this property to understand how to use this property
borderboolean|stringfalseDisplay border on the outer layer of the page? Used to distinguish centered small layout and background, can set color string
disableDesktopbooleanfalseWhen enabled, desktop adaptation is disabled. Requires enableMediaQuery to be enabled before using this parameter
disableLandscapebooleanfalseWhen enabled, mobile landscape adaptation is disabled. Requires enableMediaQuery to be enabled before using this parameter
disableMobilebooleanfalseWhen enabled, mobile portrait adaptation is disabled, converting px to viewport units like vw
excludeRegExp|RegExp[]/Exclude files or folders
includeRegExp|RegExp[]/Include files or folders
unitPrecisionnumber3How many decimal places should the unit be accurate to?
propListstring[]['*']Which properties to replace and which to ignore? Usage reference: postcss-px-to-viewport documentation
selectorBlackList(string|RegExp)[][]Selector blacklist, those on the list will not be converted
propertyBlackListpropertyBlackList[]Property blacklist, those on the list will not be converted. To specify properties within selectors, use object keys to represent selector names. See vant example code for specific usage
valueBlackList(string|RegExp)[][]Property value blacklist, values on the list will not be converted
rootContainingBlockSelectorList(string|RegExp)[][]Selector list where containing block is root element, same effect as marking comment /* root-containing-block */
verticalWritingSelectorList(string|RegExp)[][]Selector list for vertical writing mode, same effect as marking comment /* vertical-writing-mode */ at the top of selector
minDesktopDisplayWidthnumber/Width breakpoint, if not provided, defaults to desktopWidth value. When view is larger than this width, page width is desktop width desktopWidth. The "Principles and Input/Output Examples" section specifically introduces the trigger conditions for this value
maxLandscapeDisplayHeightnumber640Height breakpoint, when view is smaller than this height and meets certain conditions, page uses mobile landscape width. The "Principles and Input/Output Examples" section specifically introduces the trigger conditions for this value
sideany/Sidebar configuration, effective in desktop media queries, used to utilize widescreen space, its sub-properties will be introduced later
commentany/Custom comments, change comment names, its sub-properties will be introduced later
customLengthPropertyany/Used to specify custom variables (CSS variables, var(...)) that need to be added to desktop or landscape. If not specified, by default all length-related properties using custom variables will be added to desktop and landscape, its sub-properties will be introduced later
experimental.extractbooleanfalseExtract desktop and landscape style code for production environment, used for code splitting optimization, see "Notes" section for details
experimental.minDisplayWidthnumber/Limit minimum width, used together with maxDisplayWidth

Default Configuration Parameters

json
{
  "viewportWidth": 750,
  "maxDisplayWidth": null,
  "enableMediaQuery": false,
  "desktopWidth": 600,
  "landscapeWidth": 425,
  "minDesktopDisplayWidth": null,
  "maxLandscapeDisplayHeight": 640,
  "appSelector": "#app",
  "appContainingBlock": "calc",
  "necessarySelectorWhenAuto": null,
  "border": false,
  "disableDesktop": false,
  "disableLandscape": false,
  "disableMobile": false,
  "exclude": null,
  "include": null,
  "unitPrecision": 3,
  "selectorBlackList": [],
  "valueBlackList": [],
  "rootContainingBlockSelectorList": [],
  "verticalWritingSelectorList": [],
  "propList": ["*"],
  "mobileUnit": "vw",
  "side": {
    "width": null,
    "gap": 18,
    "selector1": null,
    "selector2": null,
    "selector3": null,
    "selector4": null,
    "width1": null,
    "width2": null,
    "width3": null,
    "width4": null
  },
  "comment": {
    "applyWithoutConvert": "apply-without-convert",
    "rootContainingBlock": "root-containing-block",
    "notRootContainingBlock": "not-root-containing-block",
    "ignoreNext": "mobile-ignore-next",
    "ignoreLine": "mobile-ignore",
    "verticalWritingMode": "vertical-writing-mode"
  },
  "customLengthProperty": {
    "rootContainingBlockList_LR": [],
    "rootContainingBlockList_NOT_LR": [],
    "ancestorContainingBlockList": [],
    "disableAutoApply": false
  },
  "experimental": {
    "extract": false,
    "minDisplayWidth": null
  }
}

Released under the MIT License.