{"id":2027,"date":"2021-11-24T11:22:07","date_gmt":"2021-11-24T19:22:07","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/surface-duo\/?p=2027"},"modified":"2021-11-24T11:22:07","modified_gmt":"2021-11-24T19:22:07","slug":"foldable-css-javascript-edge-96","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/surface-duo\/foldable-css-javascript-edge-96\/","title":{"rendered":"Foldable CSS and JavaScript update for web developers"},"content":{"rendered":"<p>\n  Hello web developers!\n<\/p>\n<p>\n  The dual-screen CSS and JavaScript enhancements for Microsoft Edge were initially <a href=\"https:\/\/devblogs.microsoft.com\/surface-duo\/dual-screen-web-experiences-preview\/\">shared in June 2020<\/a> when they were first available behind a feature flag for testing. Since then there has been a lot of work and feedback on the proposals, and the implementation currently available in the latest version of Microsoft Edge (version 96) has been updated to reflect the final API design. \n<\/p>\n<p>\n  The changes are described below, and the documentation is up to date for the <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/web\/css-viewport-segments\">latest CSS<\/a> and <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/web\/javascript-viewport-segments\">latest JavaScript<\/a> syntax. These features are still behind a <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/web\/emulator-device-testing\">feature flag which you can enable for testing<\/a>.\n<\/p>\n<h2>CSS<\/h2>\n<p>\n  The original CSS proposal provided for three different modes \u2013 but they were not easily extensible beyond a single fold. The updated syntax uses a segment count for more flexibility.\n<\/p>\n<p><strong>OLD<\/strong>\n<\/p>\n<pre>@media (screen-spanning: &lt;value&gt;) { }<\/pre>\n<p>\n  Where the <i>value<\/i> could be:\n<\/p>\n<ul>\n<li>single-fold-vertical<\/li>\n<li>single-fold-horizontal<\/li>\n<li>none<\/li>\n<\/ul>\n<p><strong>NEW<\/strong>\n<\/p>\n<pre>@media (horizontal-viewport-segments: &lt;count&gt;) { }\r\n@media (vertical-viewport-segments: &lt;count&gt;) { }<\/pre>\n<p>\n  The count reflects the number of segments, which for Microsoft Surface Duo devices would be <strong>2<\/strong>. \n<\/p>\n<p>\n  <img decoding=\"async\" width=\"1083\" height=\"404\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/word-image-1.png\" class=\"wp-image-2028\" alt=\"Diagrams showing a stylized Surface Duo device in horizontal and vertical orientation, as well as a single-screen device. The number of segments is shown below each device - two for Surface Duo and one for the single-screen device.\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/word-image-1.png 1083w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/word-image-1-300x112.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/word-image-1-1024x382.png 1024w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/word-image-1-768x286.png 768w\" sizes=\"(max-width: 1083px) 100vw, 1083px\" \/>\n<\/p>\n<h2>Environment variables<\/h2>\n<p>\n  The environment variables populated on foldable devices also needed to be updated, and introduced the concept of indexing to rows and columns of segments.\n<\/p>\n<p><strong>OLD<\/strong>\n<\/p>\n<pre>env(fold-top)\r\nenv(fold-left)\r\nenv(fold-width)\r\nenv(fold-height)<\/pre>\n<p><strong>NEW<\/strong>\n<\/p>\n<pre>env(viewport-segment-width &lt;x&gt; &lt;y&gt;)\r\nenv(viewport-segment-height &lt;x&gt; &lt;y&gt;)\r\nenv(viewport-segment-top &lt;x&gt; &lt;y&gt;)\r\nenv(viewport-segment-left &lt;x&gt; &lt;y&gt;)\r\nenv(viewport-segment-bottom &lt;x&gt; &lt;y&gt;)\r\nenv(viewport-segment-right &lt;x&gt; &lt;y&gt;)<\/pre>\n<p>\n  The x and y coordinates refer to a conceptual grid that begins with 0 0 in the top left. For Microsoft Surface Duo devices, you would use these values to use coordinates from each view segment:\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"836\" height=\"349\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-text-person-screenshot-des.png\" class=\"wp-image-2029\" alt=\"Two stylized Surface Duo devices showing the coordinate to be used with the viewport segment environment variable. The top or leftmost screen is always 0 0, the right screen is 1 0 or the bottom screen is 0 1\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-text-person-screenshot-des.png 836w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-text-person-screenshot-des-300x125.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-text-person-screenshot-des-768x321.png 768w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/>\n<\/p>\n<p>\n  You can also calculate interim values (such as the hinge dimensions):\n<\/p>\n<pre>\/* dual-portrait hinge width *\/\r\ncalc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0))\r\n\/* dual-landscape hinge height *\/\r\ncalc(env(viewport-segment-top 0 1) - env(viewport-segment-bottom 0 0))<\/pre>\n<h2>JavaScript<\/h2>\n<p>\n  The original JavaScript proposal was already capable of returning a collection of segment Rects. This behavior hasn\u2019t changed, however the method has been renamed and added to the <strong>window.visualViewport<\/strong> property.\n<\/p>\n<p><strong>OLD<\/strong>\n<\/p>\n<pre>window.getWindowSegments()<\/pre>\n<p><strong>NEW<\/strong>\n<\/p>\n<pre>window.visualViewport.segments<\/pre>\n<p>\n  The same rules apply to both versions of the API \u2013 the value is an immutable snapshot of the moment it was queried. To adapt the layout programmatically as the device posture and app mode changes, listen for <strong>resize<\/strong> or <strong>orientationchange<\/strong> events and query for the latest segment data then respond accordingly.\n<\/p>\n<h2>Example<\/h2>\n<p>\n  An example of the new CSS and JavaScript syntax can be found at this <a href=\"https:\/\/conceptdev.github.io\/web-samples\/dual-screen-css\/boxes-future.html\">GitHub URL<\/a>.\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"859\" height=\"560\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-waterfall-chart-description.png\" class=\"wp-image-2030\" alt=\"Screenshot of Edge developer tools showing the Surface Duo emulator mode and a web page with four colored boxes that have been positioned using the dual-screen web features described on this page\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-waterfall-chart-description.png 859w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-waterfall-chart-description-300x196.png 300w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2021\/11\/a-picture-containing-waterfall-chart-description-768x501.png 768w\" sizes=\"(max-width: 859px) 100vw, 859px\" \/>\n<\/p>\n<p><em>Figure: testing the dual-screen syntax in a desktop browser<\/em>\n<\/p>\n<p>\n  Here\u2019s a snippet of the CSS that\u2019s creating a hidden visualization of the fold area, which uses the media query and environment variables:\n<\/p>\n<pre>@media (horizontal-viewport-segments:2) {\r\n   .fold {\r\n     top: 0;\r\n     height: env(viewport-segment-height 0 0);\r\n     width: calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0));\r\n     left:  env(viewport-segment-right 0 0);\r\n     position: absolute;\r\n   }<\/pre>\n<p>\n  You can view the complete <a href=\"https:\/\/github.com\/conceptdev\/web-samples\/blob\/master\/dual-screen-css\/boxstyles-future.css\">CSS<\/a> and <a href=\"https:\/\/github.com\/conceptdev\/web-samples\/blob\/master\/dual-screen-css\/boxes-future.html\">HTML<\/a> source for this example on GitHub.\n<\/p>\n<h2>Testing<\/h2>\n<p>\n  You can test the updated syntax by downloading the latest versions of Edge for Surface Duo, or the desktop version of Edge or Chrome (for the <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/web\/desktop-developer-tools\">devtools simulator<\/a>) and enabling the feature-flag via <strong>edge:\/\/flags<\/strong> (or <strong>chrome:\/\/flags<\/strong> on Chrome).\n<\/p>\n<h2>Resources and feedback<\/h2>\n<p>\n  Visit docs.microsoft.com for the latest <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/web\/css-viewport-segments\">latest CSS<\/a> and <a href=\"https:\/\/docs.microsoft.com\/dual-screen\/web\/javascript-viewport-segments\">JavaScript<\/a> documentation, and this <a href=\"https:\/\/github.com\/conceptdev\/web-samples\/blob\/master\/dual-screen-css\/boxes-future.html\">example on GitHub<\/a> (<a href=\"https:\/\/github.com\/conceptdev\/web-samples\/blob\/master\/dual-screen-css\/boxstyles-future.css\">css<\/a>).\n<\/p>\n<p>\n  If you have any questions, or would like to tell us about your dual-screen web apps, use the\u00a0<a href=\"http:\/\/aka.ms\/SurfaceDuoSDK-Feedback\">feedback forum<\/a>\u00a0or message us on Twitter\u00a0<a href=\"https:\/\/twitter.com\/surfaceduodev\">@surfaceduodev<\/a>.\u00a0\n<\/p>\n<p>\n  Finally, please join us for our <a href=\"https:\/\/twitch.tv\/surfaceduodev\">dual screen developer livestream<\/a> at 11am (Pacific time) each Friday \u2013 mark it in your calendar and check out the <a href=\"https:\/\/www.youtube.com\/channel\/UClGu9QLtPNz8OdddBfhZXPA\">archives on YouTube<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello web developers! The dual-screen CSS and JavaScript enhancements for Microsoft Edge were initially shared in June 2020 when they were first available behind a feature flag for testing. Since then there has been a lot of work and feedback on the proposals, and the implementation currently available in the latest version of Microsoft Edge [&hellip;]<\/p>\n","protected":false},"author":570,"featured_media":2030,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[709,412,46,530],"class_list":["post-2027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-surface-duo-sdk","tag-design","tag-javascript","tag-surface-duo","tag-web"],"acf":[],"blog_post_summary":"<p>Hello web developers! The dual-screen CSS and JavaScript enhancements for Microsoft Edge were initially shared in June 2020 when they were first available behind a feature flag for testing. Since then there has been a lot of work and feedback on the proposals, and the implementation currently available in the latest version of Microsoft Edge [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/2027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/users\/570"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/comments?post=2027"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/2027\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media\/2030"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media?parent=2027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/categories?post=2027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/tags?post=2027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}