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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { render, screen, fireEvent } from '@testing-library/vue';
import VueRouter from 'vue-router';
import ClickableRegion from '../index.vue';

describe('ClickableRegion', () => {
it('renders a button with the given aria-label', () => {
render(ClickableRegion, {
props: {
ariaLabel: 'Test label',
},
routes: new VueRouter(),
});

expect(screen.getByRole('button', { name: 'Test label' })).toBeInTheDocument();
});

it('does not render the button when suppressed is true', () => {
render(ClickableRegion, {
props: {
ariaLabel: 'Test label',
suppressed: true,
},
routes: new VueRouter(),
});

expect(screen.queryByRole('button')).not.toBeInTheDocument();
});

it('emits a single click event on mouse click', async () => {
const { emitted } = render(ClickableRegion, {
props: {
ariaLabel: 'Test label',
},
routes: new VueRouter(),
});

await fireEvent.click(screen.getByRole('button'));

expect(emitted().click).toHaveLength(1);
});

it('emits a single click event on Enter key', async () => {
const { emitted } = render(ClickableRegion, {
props: {
ariaLabel: 'Test label',
},
routes: new VueRouter(),
});

const button = screen.getByRole('button');
await fireEvent.click(button);

expect(emitted().click).toHaveLength(1);
});

it('emits a single click event on Space key', async () => {
const { emitted } = render(ClickableRegion, {
props: {
ariaLabel: 'Test label',
},
routes: new VueRouter(),
});

const button = screen.getByRole('button');
await fireEvent.click(button);

expect(emitted().click).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<template>

<!--
a11y: Outer div catches mouse clicks.
Keyboard a11y is handled by the hidden button overlay below.
-->
<div
class="clickable-area"
@click="onClick"
Comment thread
AlexVelezLl marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just saw that this is similar to how we handle our KCard in KDS 😅, so yeah, that's fine. Could you add a comment here, please? We will introduce an a11y linter soon, and it will flag this as an incorrect pattern, but we should mute the linter instead, so it's best to add a clear comment explaining why a plain div has an @click handler without any tabindex/focus/space/enter management.

>
<button
Comment thread
AlexVelezLl marked this conversation as resolved.
v-if="!suppressed"
type="button"
class="overlay-button"
Comment thread
AlexVelezLl marked this conversation as resolved.
:aria-label="ariaLabel"
@click.stop="onClick"
></button>
<div class="content-wrapper">
<slot></slot>
</div>
</div>

</template>


<script>

export default {
name: 'ClickableRegion',
setup(props, { emit }) {
function onClick(event) {
if (props.suppressed) return;
if (event && event.stopPropagation) {
event.stopPropagation();
}
emit('click', event);
}
return { onClick };
},
props: {
ariaLabel: {
type: String,
required: true,
},
suppressed: {
type: Boolean,
default: false,
},
},
emits: ['click'],
};

</script>


<style lang="scss" scoped>

.clickable-area {
position: relative;
border-radius: inherit;
}

.overlay-button {
position: absolute;
top: 0;
left: 0;
z-index: 0;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
cursor: pointer;
background: transparent;
border: 0;
border-radius: inherit;
outline: none;

&:hover {
background-color: v-bind('$themeTokens.fineLine');
}

&:focus-visible {
outline: 2px solid v-bind('$themeTokens.focusOutline');
outline-offset: 2px;
}
}

.content-wrapper {
position: relative;
z-index: 1;
}

</style>
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
</div>

<!-- Prompt -->
<div
:class="promptWrapperClass"
<ClickableRegion
:class="getPromptWrapperClass()"
:style="promptWrapperStyle"
:suppressed="mode !== 'edit' || isQuestionOpen"
:aria-label="editQuestionLabel$()"
@click="handlePromptClick"
>
<div class="choice-card-text is-closed">
Expand All @@ -42,14 +44,15 @@
:minHeight="'80px'"
:autofocus="mode === 'edit' && isQuestionOpen"
:imageProcessor="EditorImageProcessor"
:tabindex="-1"
class="editor"
@update="setPrompt"
@minimize="closeQuestion"
/>
</div>
</div>
</div>
</div>
</ClickableRegion>
</div>

<!-- Choice list -->
Expand Down Expand Up @@ -90,11 +93,13 @@
class="choice-group"
>
<!-- Bordered choice card -->
<div
<ClickableRegion
class="choice-border"
:class="getChoiceClasses(choice)"
:style="getChoiceStyle(choice)"
@click="handleChoiceClick($event, choice.id)"
:suppressed="mode !== 'edit' || isChoiceOpen(choice.id)"
:aria-label="editAnswerOptionLabel$({ number: index + 1 })"
@click="handleChoiceClick(choice.id)"
>
<div
class="choice-card-text"
Expand Down Expand Up @@ -152,6 +157,7 @@
:minHeight="'80px'"
:autofocus="isChoiceOpen(choice.id)"
:imageProcessor="EditorImageProcessor"
:tabindex="-1"
class="editor"
@update="html => setChoiceContent(choice.id, html)"
@minimize="closeChoice"
Expand Down Expand Up @@ -181,7 +187,7 @@
>
{{ errorDuplicateChoiceContent$() }}
</ValidationMessage>
</div>
</ClickableRegion>
</div>
</component>

Expand All @@ -200,7 +206,7 @@

<script>

import { computed, ref, watch, getCurrentInstance } from 'vue';
import { computed, ref, watch } from 'vue';
import Teleport from 'vue2-teleport';
import useKResponsiveWindow from 'kolibri-design-system/lib/composables/useKResponsiveWindow';
import { themePalette, themeTokens } from 'kolibri-design-system/lib/styles/theme';
Expand All @@ -211,6 +217,7 @@
import CollapsibleToolbar from '../../components/CollapsibleToolbar/index.vue';
import ValidationMessage from '../../components/ValidationMessage/index.vue';
import AddListItemButton from '../../components/AddListItemButton/index.vue';
import ClickableRegion from '../../components/ClickableRegion/index.vue';
import AnswerSettings from './components/AnswerSettings/index.vue';
import TipTapEditor from 'shared/views/TipTapEditor/TipTapEditor/TipTapEditor';
import EditorImageProcessor from 'shared/views/TipTapEditor/TipTapEditor/services/imageService';
Expand All @@ -219,6 +226,7 @@
name: 'ChoiceInteractionEditor',

components: {
ClickableRegion,
TipTapEditor,
CollapsibleToolbar,
ValidationMessage,
Expand All @@ -245,6 +253,8 @@
answersLabel$,
answersDescriptionSingleChoice$,
answersDescriptionMultipleChoice$,
editQuestionLabel$,
editAnswerOptionLabel$,
} = qtiEditorStrings;

const palette = themePalette();
Expand Down Expand Up @@ -278,20 +288,16 @@
openChoiceId.value = null;
}

function handlePromptClick(event) {
function handlePromptClick() {
if (props.mode !== 'edit') return;
if (event.target.closest('button') || event.target.closest('input')) return;
if (!isQuestionOpen.value) {
event.stopPropagation();
openQuestion();
}
}

function handleChoiceClick(event, choiceId) {
function handleChoiceClick(choiceId) {
if (props.mode !== 'edit') return;
if (openChoiceId.value === choiceId) return;
if (event.target.closest('button') || event.target.closest('input')) return;
event.stopPropagation();
openChoice(choiceId);
}

Expand Down Expand Up @@ -325,7 +331,6 @@
{ immediate: true },
);

// Emit bodyXml and responseDeclarations whenever either changes.
const workingInteraction = computed(() => ({
bodyXml: bodyXml.value,
responseDeclarations: responseDeclarations.value,
Expand Down Expand Up @@ -419,21 +424,21 @@
];
}

const instance = getCurrentInstance();

const isPromptEditing = computed(() => props.mode === 'edit' && isQuestionOpen.value);

const promptWrapperClass = computed(() => {
return isPromptEditing.value ? 'choice-editor__prompt-wrap' : 'choice-border';
});
function getPromptWrapperClass() {
if (isPromptEditing.value) {
return 'choice-editor__prompt-wrap';
}
return ['choice-border', { 'is-clickable': props.mode === 'edit' }];
}

const promptWrapperStyle = computed(() => {
if (isPromptEditing.value) {
return {};
}
return {
borderColor: questionHasError.value ? tokens.error : tokens.fineLine,
cursor: props.mode === 'edit' ? 'pointer' : undefined,
};
});

Expand All @@ -450,18 +455,9 @@
}

function getChoiceClasses(choice) {
const closed = isChoiceClosed(choice.id);
const clickable = props.mode === 'edit' && closed;
const isCorrect = choice.correct && (props.mode === 'edit' || props.showAnswers);
const hoverBg = isCorrect ? palette.green.v_100 : tokens.fineLine;
return [
{ 'is-clickable': clickable },
clickable
? instance.proxy.$computedClass({
':hover': { backgroundColor: hoverBg },
})
: '',
];
return {
'is-clickable': props.mode === 'edit' && isChoiceClosed(choice.id),
};
}

function getChoiceStyle(choice) {
Expand All @@ -475,17 +471,20 @@
borderColor = palette.green.v_500;
}

const hoverBg = isCorrect ? palette.green.v_100 : tokens.fineLine;

return {
borderColor,
backgroundColor: isCorrect ? palette.green.v_50 : null,
'--clickable-region-hover-bg': hoverBg,
};
}

const answersHeaderId = generateRandomSlug('answers-header');

return {
EditorImageProcessor,
promptWrapperClass,
getPromptWrapperClass,
promptWrapperStyle,
state,
isSingleSelect,
Expand Down Expand Up @@ -524,6 +523,8 @@
errorEmptyChoiceContent$,
errorDuplicateChoiceContent$,
questionLabel$,
editQuestionLabel$,
editAnswerOptionLabel$,
};
},

Expand Down Expand Up @@ -707,6 +708,10 @@

.choice-border.is-clickable {
cursor: pointer;

&:hover {
background-color: var(--clickable-region-hover-bg, v-bind('$themeTokens.fineLine'));
}
}

</style>
Loading
Loading