Skip to content

Commit

Permalink
refactor: context menu positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Aug 25, 2023
1 parent c1b04e3 commit 535a762
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
1 change: 0 additions & 1 deletion packages/machines/menu/src/menu.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
const popperStyles = getPlacementStyles({
...state.context.positioning,
placement: state.context.currentPlacement,
anchorPoint: state.context.anchorPoint,
})

const api = {
Expand Down
36 changes: 16 additions & 20 deletions packages/machines/menu/src/menu.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function machine(userContext: UserDefinedContext) {

watch: {
isSubmenu: "setSubmenuPlacement",
anchorPoint: "applyAnchorPoint",
anchorPoint: "setPositioning",
},

on: {
Expand Down Expand Up @@ -388,34 +388,30 @@ export function machine(userContext: UserDefinedContext) {
clearAnchorPoint(ctx) {
ctx.anchorPoint = null
},
applyAnchorPoint(ctx) {
const point = ctx.anchorPoint
if (!point) return

const el = dom.getPositionerEl(ctx)
if (!el) return

raf(() => {
Object.assign(el.style, {
position: "absolute",
top: "0",
left: "0",
transform: `translate3d(${point.x}px, ${point.y}px, 0)`,
})
})
},
setSubmenuPlacement(ctx) {
if (!ctx.isSubmenu) return
ctx.positioning.placement = ctx.isRtl ? "left-start" : "right-start"
ctx.positioning.gutter = 0
},
setPositioning(ctx, evt) {
const getPositionerEl = () => dom.getPositionerEl(ctx)
getPlacement(dom.getTriggerEl(ctx), getPositionerEl, {

const virtualAnchorEl = ctx.anchorPoint
? {
getBoundingClientRect: () => {
const win = dom.getWin(ctx)
return win.DOMRect.fromRect({ width: 0, height: 0, ...ctx.anchorPoint })
},
}
: undefined

const anchorEl = virtualAnchorEl ?? dom.getTriggerEl(ctx)

void getPlacement(anchorEl, getPositionerEl, {
...ctx.positioning,
...evt.options,
defer: true,
...(evt.options ?? {}),
listeners: false,
// defer: true,
})
},
invokeOnValueChange(ctx, evt) {
Expand Down
13 changes: 4 additions & 9 deletions packages/utilities/popper/src/get-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ const ARROW_FLOATING_STYLE = {
right: "rotate(315deg)",
} as const

type PlacementStyleOptions = PositioningOptions & {
anchorPoint?: { x: number; y: number } | null
}

export function getPlacementStyles(options: PlacementStyleOptions = {}) {
const { placement = "bottom", sameWidth, fitViewport, strategy = "absolute", anchorPoint } = options
const point = anchorPoint || { x: 0, y: 0 }
export function getPlacementStyles(options: PositioningOptions = {}) {
const { placement = "bottom", sameWidth, fitViewport, strategy = "absolute" } = options

return {
arrow: {
Expand Down Expand Up @@ -47,8 +42,8 @@ export function getPlacementStyles(options: PlacementStyleOptions = {}) {
width: sameWidth ? "var(--reference-width)" : undefined,
maxWidth: fitViewport ? "var(--available-width)" : undefined,
maxHeight: fitViewport ? "var(--available-height)" : undefined,
top: `${point.y}px`,
left: `${point.x}px`,
top: "0px",
left: "0px",
transform: `translate3d(var(--x), var(--y), 0)`,
} as const,
}
Expand Down

4 comments on commit 535a762

@vercel
Copy link

@vercel vercel bot commented on 535a762 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 535a762 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-vue – ./examples/vue-ts

zag-vue.vercel.app
zag-vue-git-main-chakra-ui.vercel.app
zag-vue-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 535a762 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-nextjs – ./examples/next-ts

zag-two.vercel.app
zag-nextjs-chakra-ui.vercel.app
zag-nextjs-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 535a762 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zag-solid – ./examples/solid-ts

zag-solid.vercel.app
zag-solid-git-main-chakra-ui.vercel.app
zag-solid-chakra-ui.vercel.app

Please sign in to comment.