-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(browser): iframe scale #9745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 19 commits
3893ec6
fadca47
1470e91
5ea6c4c
6585d1e
0e51888
99fa6e8
4a90101
4f244e5
a775b87
25b9d8a
7f62bf7
7aaee84
e3c0566
99be09e
bae1fc6
fdbc616
cc7e0ce
83b5def
b95dbbe
79d53e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import type { UserEventCommand } from './utils' | ||
|
|
||
| export const viewport: UserEventCommand<(options: { | ||
| width: number | ||
| height: number | ||
| }) => void> = async (context, options) => { | ||
| await context.page.setViewportSize(options) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| <script setup lang="ts"> | ||
| import type { ViewportSize } from '~/composables/browser' | ||
| import { useWindowSize } from '@vueuse/core' | ||
| import { computed } from 'vue' | ||
| import { computed, onMounted, onUnmounted, ref, useTemplateRef } from 'vue' | ||
| import { viewport } from '~/composables/browser' | ||
| import { browserState } from '~/composables/client' | ||
| import { | ||
|
|
@@ -24,56 +23,42 @@ function isViewport(name: ViewportSize) { | |
| return viewport.value[0] === preset[0] && viewport.value[1] === preset[1] | ||
| } | ||
|
|
||
| const { width: windowWidth, height: windowHeight } = useWindowSize() | ||
|
|
||
| async function changeViewport(name: ViewportSize) { | ||
| viewport.value = sizes[name] | ||
| if (browserState?.provider === 'webdriverio') { | ||
| updateBrowserPanel() | ||
| } | ||
| } | ||
|
|
||
| const PADDING_SIDES = 20 | ||
| const PADDING_TOP = 100 | ||
|
|
||
| const containerSize = computed(() => { | ||
| if (browserState?.provider === 'webdriverio') { | ||
| const [width, height] = viewport.value | ||
| return { width, height } | ||
| } | ||
| const testContainer = useTemplateRef('tester-ui') | ||
| const testContainerRect = ref<DOMRectReadOnly | null>(null) | ||
|
|
||
| const parentContainerWidth = windowWidth.value * (panels.details.size / 100) | ||
| const parentOffsetWidth = parentContainerWidth * (panels.details.browser / 100) | ||
| const containerWidth = parentOffsetWidth - PADDING_SIDES | ||
| const containerHeight = windowHeight.value - PADDING_TOP | ||
| return { | ||
| width: containerWidth, | ||
| height: containerHeight, | ||
| } | ||
| const observer = new ResizeObserver(([entry]) => { | ||
| testContainerRect.value = entry.contentRect | ||
| }) | ||
|
|
||
| const scale = computed(() => { | ||
| if (browserState?.provider === 'webdriverio') { | ||
| return 1 | ||
| onMounted(() => { | ||
| if (testContainer.value) { | ||
| observer.observe(testContainer.value) | ||
| } | ||
|
|
||
| const [iframeWidth, iframeHeight] = viewport.value | ||
| const { width: containerWidth, height: containerHeight } = containerSize.value | ||
| const widthScale = containerWidth > iframeWidth ? 1 : containerWidth / iframeWidth | ||
| const heightScale = containerHeight > iframeHeight ? 1 : containerHeight / iframeHeight | ||
| return Math.min(1, widthScale, heightScale) | ||
| }) | ||
|
|
||
| const marginLeft = computed(() => { | ||
| const containerWidth = containerSize.value.width | ||
| const iframeWidth = viewport.value[0] | ||
| const offset = Math.trunc((containerWidth + PADDING_SIDES - iframeWidth) / 2) | ||
| return `${offset}px` | ||
| onUnmounted(() => { | ||
| observer.disconnect() | ||
| }) | ||
|
|
||
| const scale = computed(() => | ||
| testContainerRect.value | ||
| ? Math.floor( | ||
| Math.min( | ||
| testContainerRect.value.width / viewport.value[0], | ||
| testContainerRect.value.height / viewport.value[1], | ||
| ) * 100, | ||
| ) | ||
| : 100, | ||
| ) | ||
| </script> | ||
|
|
||
| <template> | ||
| <div h="full" flex="~ col"> | ||
| <div id="browser-frame" h="full" flex="~ col"> | ||
| <div p="3" h-10 flex="~ gap-2" items-center bg-header border="b base"> | ||
| <IconButton | ||
| v-show="panels.navigation <= 15" | ||
|
|
@@ -118,40 +103,30 @@ const marginLeft = computed(() => { | |
| /> | ||
| <span class="pointer-events-none" text-sm> | ||
| {{ viewport[0] }}x{{ viewport[1] }}px | ||
| <span v-if="scale < 1">({{ (scale * 100).toFixed(0) }}%)</span> | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I can add this back now if necessary.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we remove it? The window is still scaled
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed it initially when I wasn't sure about what approach to go with. I'll add it back 👍🏼 |
||
| <span v-if="scale < 100">({{ scale }}%)</span> | ||
| </span> | ||
| </div> | ||
| <div id="tester-container" relative> | ||
| <div | ||
| id="tester-ui" | ||
| class="flex h-full justify-center items-center font-light op70" | ||
| :data-scale="scale" | ||
| :style="{ | ||
| '--viewport-width': `${viewport[0]}px`, | ||
| '--viewport-height': `${viewport[1]}px`, | ||
| '--tester-transform': `scale(${scale})`, | ||
| '--tester-margin-left': marginLeft, | ||
| }" | ||
| > | ||
| Select a test to run | ||
| </div> | ||
| <div id="tester-ui" ref="tester-ui"> | ||
| Select a test to run | ||
| </div> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped> | ||
| #tester-container:not([data-ready]) { | ||
| width: 100%; | ||
| #tester-ui { | ||
| height: 100%; | ||
| container-type: size; | ||
|
|
||
| margin-top: 0.5rem; | ||
| } | ||
|
|
||
| #tester-ui:not([data-ready]) { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| [data-ready] #tester-ui { | ||
| width: var(--viewport-width); | ||
| height: var(--viewport-height); | ||
| transform: var(--tester-transform); | ||
| margin-left: var(--tester-margin-left); | ||
| opacity: 0.7; | ||
|
|
||
| font-weight: 300; | ||
| } | ||
| </style> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| import { ref } from 'vue' | ||
| import { ref, watch } from 'vue' | ||
|
|
||
| export type ViewportSize | ||
| = | 'small-mobile' | ||
| | 'large-mobile' | ||
| | 'tablet' | ||
| export const viewport = ref<[number, number]>([414, 896]) | ||
|
|
||
| watch([viewport], () => { | ||
| document.body.style.setProperty('--viewport-width', `${viewport.value[0]}px`) | ||
| document.body.style.setProperty('--viewport-height', `${viewport.value[1]}px`) | ||
| }, { immediate: true, flush: 'sync' }) |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an explanation (as a comment) of what is going on here? I've never seen this usage before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the comment, does this version explain it better? 🤔