chore: update LabelConfig name, add to store, create a11y example
This commit is contained in:
@@ -2,7 +2,6 @@ import { CSSProperties } from 'react';
|
||||
|
||||
import { useStore } from '../../hooks/useStore';
|
||||
import type { ReactFlowState } from '../../types';
|
||||
import type { descriptions } from '@xyflow/system';
|
||||
|
||||
const style: CSSProperties = { display: 'none' };
|
||||
const ariaLiveStyle: CSSProperties = {
|
||||
@@ -21,19 +20,11 @@ export const ARIA_NODE_DESC_KEY = 'react-flow__node-desc';
|
||||
export const ARIA_EDGE_DESC_KEY = 'react-flow__edge-desc';
|
||||
export const ARIA_LIVE_MESSAGE = 'react-flow__aria-live';
|
||||
|
||||
const defaultDescriptions: Required<descriptions> = {
|
||||
'a11yDescription.node.default':
|
||||
'Press enter or space to select a node. Press delete to remove it and escape to cancel.',
|
||||
'a11yDescription.node.keyboardDisabled':
|
||||
'Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.',
|
||||
'a11yDescription.edge.default':
|
||||
'Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.',
|
||||
};
|
||||
|
||||
const selector = (s: ReactFlowState) => s.ariaLiveMessage;
|
||||
const ariaLiveSelector = (s: ReactFlowState) => s.ariaLiveMessage;
|
||||
const labelConfigSelector = (s: ReactFlowState) => s.labelConfig;
|
||||
|
||||
function AriaLiveMessage({ rfId }: { rfId: string }) {
|
||||
const ariaLiveMessage = useStore(selector);
|
||||
const ariaLiveMessage = useStore(ariaLiveSelector);
|
||||
|
||||
return (
|
||||
<div id={`${ARIA_LIVE_MESSAGE}-${rfId}`} aria-live="assertive" aria-atomic="true" style={ariaLiveStyle}>
|
||||
@@ -42,20 +33,13 @@ function AriaLiveMessage({ rfId }: { rfId: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
export function A11yDescriptions({
|
||||
rfId,
|
||||
disableKeyboardA11y,
|
||||
descriptions = {},
|
||||
}: {
|
||||
rfId: string;
|
||||
disableKeyboardA11y: boolean;
|
||||
descriptions?: descriptions;
|
||||
}) {
|
||||
export function A11yDescriptions({ rfId, disableKeyboardA11y }: { rfId: string; disableKeyboardA11y: boolean }) {
|
||||
const labelConfig = useStore(labelConfigSelector);
|
||||
|
||||
const nodeDesc = disableKeyboardA11y
|
||||
? descriptions['a11yDescription.node.default'] || defaultDescriptions['a11yDescription.node.default']
|
||||
: descriptions['a11yDescription.node.keyboardDisabled'] ||
|
||||
defaultDescriptions['a11yDescription.node.keyboardDisabled'];
|
||||
const edgeDesc = descriptions['a11yDescription.edge.default'] || defaultDescriptions['a11yDescription.edge.default'];
|
||||
? labelConfig['a11yDescription.node.default']
|
||||
: labelConfig['a11yDescription.node.keyboardDisabled'];
|
||||
const edgeDesc = labelConfig['a11yDescription.edge.default'];
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
@@ -145,7 +145,7 @@ function ReactFlow<NodeType extends Node = Node, EdgeType extends Edge = Edge>(
|
||||
colorMode = 'light',
|
||||
debug,
|
||||
onScroll,
|
||||
descriptions,
|
||||
labelConfig,
|
||||
...rest
|
||||
}: ReactFlowProps<NodeType, EdgeType>,
|
||||
ref: ForwardedRef<HTMLDivElement>
|
||||
@@ -310,7 +310,7 @@ function ReactFlow<NodeType extends Node = Node, EdgeType extends Edge = Edge>(
|
||||
<SelectionListener<NodeType, EdgeType> onSelectionChange={onSelectionChange} />
|
||||
{children}
|
||||
<Attribution proOptions={proOptions} position={attributionPosition} />
|
||||
<A11yDescriptions rfId={rfId} disableKeyboardA11y={disableKeyboardA11y} descriptions={descriptions} />
|
||||
<A11yDescriptions rfId={rfId} disableKeyboardA11y={disableKeyboardA11y} />
|
||||
</Wrapper>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
NodeOrigin,
|
||||
CoordinateExtent,
|
||||
fitViewport,
|
||||
LabelConfig,
|
||||
} from '@xyflow/system';
|
||||
|
||||
import { applyEdgeChanges, applyNodeChanges, createSelectionChange, getSelectionChanges } from '../utils/changes';
|
||||
@@ -370,6 +371,11 @@ const createStore = ({
|
||||
},
|
||||
|
||||
reset: () => set({ ...getInitialState() }),
|
||||
updateLabelConfig: (newLabelConfig: Partial<LabelConfig>) => {
|
||||
set((state) => ({
|
||||
labelConfig: { ...state.labelConfig, ...newLabelConfig },
|
||||
}));
|
||||
},
|
||||
};
|
||||
}, Object.is);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
NodeOrigin,
|
||||
initialConnection,
|
||||
CoordinateExtent,
|
||||
defaultLabelConfig,
|
||||
} from '@xyflow/system';
|
||||
|
||||
import type { Edge, FitViewOptions, InternalNode, Node, ReactFlowStore } from '../types';
|
||||
@@ -141,6 +142,7 @@ const getInitialState = ({
|
||||
|
||||
lib: 'react',
|
||||
debug: false,
|
||||
labelConfig: defaultLabelConfig,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import type {
|
||||
ColorMode,
|
||||
SnapGrid,
|
||||
OnReconnect,
|
||||
descriptions,
|
||||
LabelConfig,
|
||||
} from '@xyflow/system';
|
||||
|
||||
import type {
|
||||
@@ -671,5 +671,5 @@ export interface ReactFlowProps<NodeType extends Node = Node, EdgeType extends E
|
||||
* Custom accessibility messages for screen readers and a11y features.
|
||||
* Allows localization and customization of ARIA descriptions.
|
||||
*/
|
||||
descriptions?: descriptions;
|
||||
labelConfig?: LabelConfig;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
type NodeChange,
|
||||
type EdgeChange,
|
||||
type ParentLookup,
|
||||
type LabelConfig,
|
||||
} from '@xyflow/system';
|
||||
|
||||
import type {
|
||||
@@ -67,7 +68,6 @@ export type ReactFlowStore<NodeType extends Node = Node, EdgeType extends Edge =
|
||||
domNode: HTMLDivElement | null;
|
||||
paneDragging: boolean;
|
||||
noPanClassName: string;
|
||||
|
||||
panZoom: PanZoomInstance | null;
|
||||
minZoom: number;
|
||||
maxZoom: number;
|
||||
@@ -148,6 +148,7 @@ export type ReactFlowStore<NodeType extends Node = Node, EdgeType extends Edge =
|
||||
|
||||
lib: string;
|
||||
debug: boolean;
|
||||
labelConfig: Required<LabelConfig>;
|
||||
};
|
||||
|
||||
export type ReactFlowActions<NodeType extends Node, EdgeType extends Edge> = {
|
||||
@@ -171,6 +172,7 @@ export type ReactFlowActions<NodeType extends Node, EdgeType extends Edge> = {
|
||||
triggerEdgeChanges: (changes: EdgeChange<EdgeType>[]) => void;
|
||||
panBy: PanBy;
|
||||
setPaneClickDistance: (distance: number) => void;
|
||||
updateLabelConfig: (newLabelConfig: Partial<LabelConfig>) => void;
|
||||
};
|
||||
|
||||
export type ReactFlowState<NodeType extends Node = Node, EdgeType extends Edge = Edge> = ReactFlowStore<
|
||||
|
||||
Reference in New Issue
Block a user