Skip to content

Vue ​

The @mushafjs/vue package provides a <MushafViewer> component that wraps the core mushaf.js library. It handles lifecycle management, exposes navigation methods via template refs, and maps all callbacks to Vue events.


Installation ​

bash
npm install mushaf.js @mushafjs/vue

Basic usage ​

vue
<template>
  <MushafViewer
  />
</template>

<script setup lang="ts">
import { MushafViewer } from '@mushafjs/vue';
import 'mushaf.js/styles';
</script>

Page binding with v-model ​

Use v-model:page to keep your component state in sync with the rendered page:

vue
<template>
  <p>Page {{ page }} / 604</p>
  <MushafViewer v-model:page="page" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { MushafViewer } from '@mushafjs/vue';
import 'mushaf.js/styles';

const page = ref(1);
</script>

Expose navigation methods to your parent component using a template ref:

vue
<template>
  <div>
    <button @click="viewer?.previousPage()">← Prev</button>
    <button @click="viewer?.nextPage()">Next →</button>
    <button @click="viewer?.goToSurah(36)">Ya-Sin</button>
    <button @click="viewer?.goToAyah(2, 255)">Ayat al-Kursi</button>
    <button @click="viewer?.clearHighlight()">Clear</button>

    <MushafViewer
      ref="viewer"
      v-model:page="page"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { MushafViewer } from '@mushafjs/vue';
import 'mushaf.js/styles';

const page = ref(1);
const viewer = ref<InstanceType<typeof MushafViewer> | null>(null);
</script>

Handling events ​

All mushaf.js callbacks are exposed as Vue events using kebab-case names:

vue
<template>
  <MushafViewer
    @word-click="onWord"
    @ayah-click="onAyah"
    @surah-click="onSurah"
    @page-change="onPage"
  />
</template>

<script setup lang="ts">
import { MushafViewer } from '@mushafjs/vue';
import type { Word, Ayah, Surah } from '@mushafjs/vue';
import 'mushaf.js/styles';

function onWord(word: Word, element: HTMLElement) {
  console.log(`${word.surah}:${word.ayah}:${word.word}`);
}

function onAyah(ayah: Ayah) {
  console.log(`Ayah ${ayah.surah}:${ayah.ayah} — ${ayah.words.length} words`);
}

function onSurah(surah: Surah) {
  console.log(`Surah ${surah.number}: ${surah.name_simple}`);
}

function onPage(page: number) {
  document.title = `Page ${page} / 604`;
}
</script>

Highlighting from the parent ​

vue
<template>
  <MushafViewer
    ref="viewer"
    @ayah-click="highlightAyah"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { MushafViewer } from '@mushafjs/vue';
import type { Ayah } from '@mushafjs/vue';
import 'mushaf.js/styles';

const viewer = ref<InstanceType<typeof MushafViewer> | null>(null);

function highlightAyah(ayah: Ayah) {
  viewer.value?.highlightAyah(ayah.surah, ayah.ayah, {
    style: 'underline',
    color: '#4caf50',
  });
}
</script>

Props ​

PropTypeDefaultDescription
pagenumber1Current page (use with v-model:page).
dataBaseUrlstring'https://cdn.mushafjs.com/pages'Base URL for page JSON files (only to self-host).
fontsBaseDirstring'https://cdn.mushafjs.com/fonts'Base directory for QPC font files (only to self-host).
preloadPagesnumber3Pages to preload on init.
wordHighlightHighlightConfig—Default highlight for words.
ayahHighlightHighlightConfig—Default highlight for ayahs.
surahHighlightHighlightConfig—Default highlight for surahs.
juzHighlightHighlightConfig—Default highlight for juz.
selectionHighlightHighlightConfig{ style: 'border' }Style of the range selection.
enableWordClickbooleantrueEnable word click events.
enableAyahClickbooleantrueEnable ayah click events.
enableSurahClickbooleantrueEnable surah header click events.
enableJuzClickbooleantrueEnable juz marker click events.
fontSizenumber—Base font size in px.
fontFamilystring—CSS font-family override.
lineHeightnumber—CSS line-height override.
textColorstring—Text colour override.

Events ​

EventPayloadDescription
update:pagenumberEmitted on every page change (supports v-model:page).
page-changenumberSame as above — use when you don't need v-model.
word-click(word: Word, element: HTMLElement)A word glyph was clicked.
ayah-click(ayah: Ayah)The ayah-end marker was clicked.
surah-click(surah: Surah)A surah header was clicked.
juz-click(juz: Juz)A juz margin marker was clicked.
hizb-click(hizb: Hizb)A hizb margin marker was clicked.
rub-click(rub: Rub)A rub el-hizb marker was clicked.

Exposed methods ​

Access these methods via a template ref (see Navigation via template ref):

MethodDescription
nextPage()Navigate to the next page.
previousPage()Navigate to the previous page.
goToPage(page)Navigate to a specific page number.
goToSurah(surah)Jump to the first page of a surah.
goToAyah(surah, ayah)Jump to the page containing an ayah.
highlightWord(surah, ayah, word, config?)Highlight a single word.
highlightAyah(surah, ayah, config?)Highlight all words of an ayah.
highlightSurah(surah, config?)Highlight all words of a surah on the current page.
clearHighlight()Remove all highlights.
getCurrentPage()Returns the current page number.
getPageInfo()Returns { page, totalPages }.

TypeScript types ​

Types are re-exported from @mushafjs/vue for convenience:

ts
import type {
  HighlightConfig,
  HighlightStyle,
  Ayah,
  Surah,
  Juz,
  Hizb,
  Rub,
  Word,
  MushafConfig,
} from '@mushafjs/vue';