Menu: support ARIA attributes
This commit is contained in:
		
							parent
							
								
									8205ff661b
								
							
						
					
					
						commit
						c22860a7e3
					
				
					 2 changed files with 31 additions and 18 deletions
				
			
		| 
						 | 
				
			
			@ -23,6 +23,11 @@
 | 
			
		|||
  &.e4 {
 | 
			
		||||
    box-shadow: var(--tutu-shadow-e12);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  &>.container {
 | 
			
		||||
    background: var(--tutu-color-surface);
 | 
			
		||||
    display: contents;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dialog.Menu::backdrop {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ import { MenuList } from "@suid/material";
 | 
			
		|||
import {
 | 
			
		||||
  createEffect,
 | 
			
		||||
  createSignal,
 | 
			
		||||
  splitProps,
 | 
			
		||||
  type Component,
 | 
			
		||||
  type JSX,
 | 
			
		||||
  type ParentProps,
 | 
			
		||||
| 
						 | 
				
			
			@ -16,11 +17,15 @@ import {
 | 
			
		|||
 | 
			
		||||
export type Anchor = Pick<DOMRect, "top" | "left" | "right"> & { e?: number };
 | 
			
		||||
 | 
			
		||||
export type MenuProps = ParentProps<{
 | 
			
		||||
  open?: boolean;
 | 
			
		||||
  onClose?: JSX.EventHandlerUnion<HTMLDialogElement, Event>;
 | 
			
		||||
  anchor: () => Anchor;
 | 
			
		||||
}>;
 | 
			
		||||
export type MenuProps = ParentProps<
 | 
			
		||||
  {
 | 
			
		||||
    open?: boolean;
 | 
			
		||||
    onClose?: JSX.EventHandlerUnion<HTMLDialogElement, Event>;
 | 
			
		||||
    anchor: () => Anchor;
 | 
			
		||||
 | 
			
		||||
    id?: string;
 | 
			
		||||
  } & JSX.AriaAttributes
 | 
			
		||||
>;
 | 
			
		||||
 | 
			
		||||
function px(n?: number) {
 | 
			
		||||
  if (n) {
 | 
			
		||||
| 
						 | 
				
			
			@ -74,6 +79,7 @@ export function createManagedMenuState() {
 | 
			
		|||
const Menu: Component<MenuProps> = (props) => {
 | 
			
		||||
  let root: HTMLDialogElement;
 | 
			
		||||
  const windowSize = useWindowSize();
 | 
			
		||||
  const [, rest] = splitProps(props, ["open", "onClose", "anchor"]);
 | 
			
		||||
 | 
			
		||||
  const [anchorPos, setAnchorPos] = createSignal<{
 | 
			
		||||
    left?: number;
 | 
			
		||||
| 
						 | 
				
			
			@ -83,15 +89,16 @@ const Menu: Component<MenuProps> = (props) => {
 | 
			
		|||
 | 
			
		||||
  if (import.meta.env.DEV) {
 | 
			
		||||
    createEffect(() => {
 | 
			
		||||
      switch (anchorPos().e) {
 | 
			
		||||
        case 1:
 | 
			
		||||
        case 2:
 | 
			
		||||
        case 3:
 | 
			
		||||
        case 3:
 | 
			
		||||
          return;
 | 
			
		||||
        default:
 | 
			
		||||
          console.warn('value %s is invalid for param "e"', anchorPos().e);
 | 
			
		||||
      }
 | 
			
		||||
      if (anchorPos().e)
 | 
			
		||||
        switch (anchorPos().e) {
 | 
			
		||||
          case 1:
 | 
			
		||||
          case 2:
 | 
			
		||||
          case 3:
 | 
			
		||||
          case 4:
 | 
			
		||||
            return;
 | 
			
		||||
          default:
 | 
			
		||||
            console.warn('value %s is invalid for param "e"', anchorPos().e);
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -202,12 +209,13 @@ const Menu: Component<MenuProps> = (props) => {
 | 
			
		|||
        top: px(anchorPos().top),
 | 
			
		||||
        /* FIXME: the content may be overflow */
 | 
			
		||||
      }}
 | 
			
		||||
      role="presentation"
 | 
			
		||||
      tabIndex={-1}
 | 
			
		||||
      {...rest}
 | 
			
		||||
    >
 | 
			
		||||
      <div
 | 
			
		||||
        style={{
 | 
			
		||||
          background: "var(--tutu-color-surface)",
 | 
			
		||||
          display: "contents"
 | 
			
		||||
        }}
 | 
			
		||||
        class="container"
 | 
			
		||||
        role="presentation"
 | 
			
		||||
      >
 | 
			
		||||
        <MenuList>{props.children}</MenuList>
 | 
			
		||||
      </div>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue