Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ The component is the default export of `@opencor/plot`, and is also exported as
| `PlotImageExportEvent` | `{ action: 'copy' \| 'export'; format: 'jpeg' \| 'png' \| 'svg' \| 'webp'; success: boolean }` | The payload of the [`imageExport`](#events) event: whether the plot was copied to the clipboard (always as PNG) or exported to an image file, in which format, and whether it worked. |
| `PlotContextMenuEvent` | `{ clientX: number; clientY: number; usingKeyboard: boolean; items: ContextMenuItem[] }` | The payload of the [`contextMenu`](#events) event: where the context menu was requested (in client coordinates), whether it was requested using the keyboard (in which case the custom menu should take the keyboard focus), and the items that our context menu would have shown. These items reflect the plot as it was when the context menu was requested and don't get updated if the data changes while the custom context menu is shown, but their commands always act on the plot as it is when they are chosen. |
| `ContextMenuItem` | `ContextMenuAction \| ContextMenuSeparator \| ContextMenuHeader` | An item of a context menu. |
| `ContextMenuAction` | `{ id?: string; label: string; icon?: IconName; checked?: boolean; command?: () => void; items?: ContextMenuItem[] }` | An action: its `id` (so that it can be found, see [Context menu items](#context-menu-items)), its `label`, its `icon`, whether it is checked (only for a checkable action, e.g., to make an axis logarithmic), the `command` to call when it is chosen, and its submenu `items`, if any. |
| `ContextMenuAction` | `{ id?: string; label: string; icon?: IconName; checked?: boolean; disabled?: boolean; command?: () => void; items?: ContextMenuItem[] }` | An action: its `id` (so that it can be found, see [Context menu items](#context-menu-items)), its `label`, its `icon`, whether it is checked (only for a checkable action, e.g., to make an axis logarithmic), whether it is disabled (i.e. greyed out, can't be chosen, and its submenu, if any, can't be opened, although it can still be focused, so that assistive technologies announce it as disabled, and it still shows whether it is checked), the `command` to call when it is chosen, and its submenu `items`, if any. |
| `ContextMenuSeparator` | `{ separator: true }` | A separator. |
| `ContextMenuHeader` | `{ header: string }` | A header that can't be chosen, e.g., the full name of a trace when the context menu is requested from its legend item. It can contain a little HTML (see [Data](#data)). |
| `IconName` | `'angle-right' \| 'arrows-h' \| 'arrows-v' \| 'chart-line' \| 'check' \| 'copy' \| 'download' \| 'eye' \| 'image' \| 'list' \| 'pencil' \| 'refresh' \| 'search-minus' \| 'search-plus'` | The name of an icon (from [PrimeIcons](https://primevue.org/icons/)). |
Expand Down Expand Up @@ -313,7 +313,7 @@ To draw traces without our level of detail, use `:levelOfDetail="false"`. Our pa
| `imageExport` | [`PlotImageExportEvent`](#types) | The plot has been copied to the clipboard or exported to an image (or has failed to be), so that the user can be told about it. |
| `rangesUpdated` | [`PlotRanges`](#types) | The range of an axis has changed, or whether it is zoomed has. It is emitted at most once per frame, including while the plot is being zoomed, panned, or animated, and while its data gets updated. |
| `update:showLegend` | `boolean` | The user has shown or hidden the legend using the context menu, so that the [`showLegend`](#props) prop can say the same (e.g., using `v-model:showLegend`). |
| `contextMenu` | [`PlotContextMenuEvent`](#types) | Only with [`customContextMenu`](#props): the context menu was requested at `clientX`/`clientY`, with the `items` that our context menu would have shown. If `usingKeyboard` is true, the custom menu should take the keyboard focus. The custom menu should call the `command` of the chosen item while handling the click (or tap, or key press) that chose it, and it shouldn't move the keyboard focus afterwards (calling [`contextMenuHidden()`](#methods) instead), since some commands (e.g., to edit the range of an axis) give the keyboard focus to an editor that closes when it loses it, and iOS only shows its virtual keyboard for such an editor while handling a user interaction. If it calls that `command` while handling a key press (e.g., Enter or Space), it should also prevent that key's default action (using `preventDefault()`), since the key would otherwise also be typed into such an editor, replacing its selected value. |
| `contextMenu` | [`PlotContextMenuEvent`](#types) | Only with [`customContextMenu`](#props): the context menu was requested at `clientX`/`clientY`, with the `items` that our context menu would have shown. The custom menu should show the actions that are `disabled` (see [`ContextMenuAction`](#types)) as such, and not let them (or their submenu items) be chosen. If `usingKeyboard` is true, the custom menu should take the keyboard focus. The custom menu should call the `command` of the chosen item while handling the click (or tap, or key press) that chose it, and it shouldn't move the keyboard focus afterwards (calling [`contextMenuHidden()`](#methods) instead), since some commands (e.g., to edit the range of an axis) give the keyboard focus to an editor that closes when it loses it, and iOS only shows its virtual keyboard for such an editor while handling a user interaction. If it calls that `command` while handling a key press (e.g., Enter or Space), it should also prevent that key's default action (using `preventDefault()`), since the key would otherwise also be typed into such an editor, replacing its selected value. |

### Methods

Expand Down Expand Up @@ -347,9 +347,19 @@ Copied and exported images always use the light colours (white background, dark

### Context menu items

The items of the context menu can be customised using the [`contextMenuItems`](#props) prop, e.g., to add an item that lets the user choose whether the X axes of some plots are synchronised, at the end of the submenu of the X axis (and separated from its other items):
The items of the context menu can be customised using the [`contextMenuItems`](#props) prop, e.g., to add an item that lets the user choose whether the X axes of some plots are synchronised, at the end of the submenu of the X axis (and separated from its other items), and which is disabled when there is no other plot to synchronise with:

```ts
import { type ContextMenuAction, type ContextMenuItem } from '@opencor/plot';

import { ref } from 'vue';

// The number of plots, which the app must keep up to date (e.g., using a computed from its list of plots), so that our
// item gets disabled when there is no other plot to synchronise with.

const nbOfPlots = ref(2);
const linkedXAxes = ref(true);

const contextMenuItems = (items: ContextMenuItem[]): ContextMenuItem[] => {
const xAxisItem = items.find((item): item is ContextMenuAction => 'id' in item && item.id === 'xAxis');

Expand All @@ -360,7 +370,15 @@ const contextMenuItems = (items: ContextMenuItem[]): ContextMenuItem[] => {
{
label: 'Linked',
checked: linkedXAxes.value,
disabled: nbOfPlots.value < 2,
command: () => {
// Note: the items don't get updated while a custom context menu (see customContextMenu) is shown, so this item
// may still be chosen after the number of plots has dropped to one, hence we check it again.

if (nbOfPlots.value < 2) {
return;
}

linkedXAxes.value = !linkedXAxes.value;
}
}
Expand Down
60 changes: 51 additions & 9 deletions demo/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@
>
<li v-for="(item, index) in customMenu.items" :key="index">
<strong v-if="item.header">{{ item.label }}</strong>
<hr v-else-if="!item.command" />
<button v-else type="button" @click="onCustomMenuItemClick(item.command)">{{ item.label }}</button>
<hr v-else-if="item.separator" />
<button v-else type="button" :aria-disabled="item.disabled ? 'true' : undefined" @click="onCustomMenuItemClick(item)">
<span>{{ item.label }}</span>
</button>
</li>
</ul>
</div>
Expand Down Expand Up @@ -916,7 +918,8 @@ const ALIGN_GROUP = Symbol('demo');
const SYNC_GROUP = Symbol('demo');

// Customise the context menu of our instances, so that the user can choose whether their X and/or Y axes are
// synchronised from there too, at the end of the submenu of each axis (and separated from its other items).
// synchronised from there too, at the end of the submenu of each axis (and separated from its other items). That item
// is disabled when there is only one instance, since there is then no other instance to synchronise with.

const contextMenuItems = (items: ContextMenuItem[]): ContextMenuItem[] => {
const linkedAxes = { xAxis: linkedXAxes, yAxis: linkedYAxes };
Expand All @@ -931,7 +934,15 @@ const contextMenuItems = (items: ContextMenuItem[]): ContextMenuItem[] => {
{
label: 'Linked',
checked: linked.value,
disabled: nbOfInstances.value < 2,
command: () => {
// Note: our items don't get updated while a custom context menu (see onContextMenu()) is shown, so this item
// may still be chosen after our number of instances has dropped to one, hence we check it again.

if (nbOfInstances.value < 2) {
return;
}

linked.value = !linked.value;
}
}
Expand Down Expand Up @@ -1673,6 +1684,8 @@ const sectionBindings = (name: string): { open: boolean; onToggle: (event: Event
interface CustomMenuItem {
label: string;
header?: boolean;
separator?: boolean;
disabled?: boolean;
command?: () => void;
}

Expand All @@ -1692,10 +1705,14 @@ const headerLabel = (header: string): string => {
return body.textContent ?? '';
};

const flattenedItems = (items: ContextMenuItem[], prefix: string = ''): CustomMenuItem[] => {
const flattenedItems = (items: ContextMenuItem[], prefix: string = '', disabled: boolean = false): CustomMenuItem[] => {
// Note #1: the items of the submenu of a disabled action are also disabled, since that submenu can't be opened.
// Note #2: a separator is flagged as such rather than being an item without a command, since an action may have no
// command (e.g., a disabled action that is only there to give some information).

return items.flatMap((item: ContextMenuItem): CustomMenuItem[] => {
if ('separator' in item) {
return [{ label: '' }];
return [{ label: '', separator: true }];
}

if ('header' in item) {
Expand All @@ -1704,7 +1721,11 @@ const flattenedItems = (items: ContextMenuItem[], prefix: string = ''): CustomMe

const label = `${prefix}${item.label}${item.checked ? ' ✓' : ''}`;

return item.items ? flattenedItems(item.items, `${label} › `) : [{ label, command: item.command }];
const itemDisabled = disabled || !!item.disabled;

return item.items
? flattenedItems(item.items, `${label} › `, itemDisabled)
: [{ label, disabled: itemDisabled, command: item.command }];
});
};

Expand Down Expand Up @@ -1734,10 +1755,18 @@ const hideCustomMenu = (): void => {
plotRefs[instance]?.contextMenuHidden();
};

const onCustomMenuItemClick = (command: () => void): void => {
const onCustomMenuItemClick = (item: CustomMenuItem): void => {
// Note: a disabled item is flagged using aria-disabled rather than the disabled attribute, so that it can still be
// focused and gets announced (as disabled) by assistive technologies, like in our context menu. This means that
// we must ignore it ourselves and keep our custom context menu open (again, like in our context menu).

if (item.disabled) {
return;
}

hideCustomMenu();

command();
item.command?.();
};

const onWindowPointerDown = (event: PointerEvent): void => {
Expand Down Expand Up @@ -2023,7 +2052,20 @@ const onWindowPointerDown = (event: PointerEvent): void => {
cursor: pointer;
}

.demo-custom-menu button:hover,
/**
* Note: only the label of a disabled item is greyed out, so that its background and focus ring are as visible as
* those of an enabled item, like in our context menu.
*/

.demo-custom-menu button[aria-disabled='true'] {
cursor: default;
}

.demo-custom-menu button[aria-disabled='true'] span {
opacity: 0.6;
}

.demo-custom-menu button:not([aria-disabled='true']):hover,
.demo-custom-menu button:focus-visible {
background-color: var(--demo-border);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"type": "module",
"types": "dist/index.d.ts",
"version": "0.20261204.1",
"version": "0.20261204.2",
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/common/markerStyleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ export const traceColor = (
};

export const markerPadding = (style: ResolvedMarkerStyle): number => {
// Return the autorange padding (in CSS pixels) for markers of the given style, i.e., like Plotly, more than half their
// size, but at least 3 pixels and, unlike Plotly, at least how far they reach from their point (e.g., because of a
// thick outline, see markerDrawing()), so that they are fully shown.
// Return the autorange padding (in CSS pixels) for markers of the given style, i.e., like Plotly, more than half
// their size, but at least 3 pixels and, unlike Plotly, at least how far they reach from their point (e.g., because
// of a thick outline, see markerDrawing()), so that they are fully shown.

return Math.max(style.size / 1.6, 3, markerDrawing(style)?.reach ?? 0);
};
Expand Down
Loading