#Dynamic fontFamily style

56 messages ยท Page 1 of 1 (latest)

surreal pike
#

I want to apply a fontFamily on an element dynamically, but it wouldn't appear in the browser dev tools inspection, only the font-size appears

#
import { Box, Title, Text, Container } from '@mantine/core';
import { useTaskVerses, useWordSound } from '../../hooks';
// import { useTaskVerses } from './hooks';

const WBW = () => {
  const { pageNumbers, verses } = useTaskVerses();
  // console.log(`Mushaf Page ${pageNumbers[0]}`);
  const font = verses && `Mushaf Page ${pageNumbers![0]}`;
  console.log('the font is', font);

  const { playWord } = useWordSound(verses);

  if (!verses) return 'loading..';
  // const { pageNumbers } = useTaskVerses();
  console.log({ pageNumbers });
  return (
    <Container>
      {pageNumbers?.map((page) => (
        <style key={page}>
          {`
          @font-face {
            font-family: 'Mushaf Page ${page}';
            src: url('fonts/mushaf-woff2/QCF_P${page}.woff2')
          }
          `}
        </style>
      ))}
      <Title>ุงู„ู…ุทู„ูˆุจ ุงู„ูŠูˆู…ูŠ - ูƒู„ู…ุฉ ุจูƒู„ู…ุฉ</Title>
      {verses && (
        <div
          style={{
            // fontFamily: font,
            fontFamily: 'Mushaf Page 487',
            fontSize: '1.8rem',
          }}
        >
          {verses?.map((verse) => (
            <Text key={verse.id}>
              {verse.words.map((word) => (
                <Text
                  span
                  key={word.id}
                  style={{ fontFamily: 'Mushaf Page 487' }}
                  onClick={() => playWord(word.id)}
                >
                  {word.code_v1}
                </Text>
              ))}
            </Text>
          ))}
        </div>
      )}
    </Container>
  );
};

export default WBW;
#

useTaskVerses definition.

import { useQuery } from '@tanstack/react-query';
import axios from 'axios';

const useTaskVerses = () => {
  const { data } = useQuery({
    queryKey: ['task-verses'],
    queryFn: () =>
      axios.get(import.meta.env.VITE_SERVER_URL + 'daily-task/wbw'),
  });
  const verses = data?.data.verses;
  console.log({ verses });

  const pageNumbers: number[] | undefined =
    data &&
    ([...new Set(verses.map((verse) => verse.page_number))] as
      | number[]
      | undefined);

  return {
    verses,
    pageNumbers,
  };
};

export default useTaskVerses;
craggy ferry
#

font-family: 'Mushaf Page ${page}';

#

It does not pick up the page

#

but rather, just the string ${page}

surreal pike
#

in this?
{pageNumbers?.map((page) => (
<style key={page}>
{ @font-face { font-family: 'Mushaf Page ${page}'; src: url('fonts/mushaf-woff2/QCF_P${page}.woff2') } }

craggy ferry
#

Yes

#

To use ${page} you need to wrap the string with a ` instead of '

surreal pike
#

no, that is not the problem

#

the font face definition get to the browser successfully

craggy ferry
#

Oh yes I see

#

Did not see you already wrapping with it

#

Are you using mantine v6?

surreal pike
#

v7

craggy ferry
#

What if you try a different font, because it seems weird that its not applying on a default html element

surreal pike
#

different font gets applied successfully

craggy ferry
#

What if you try Mushaf Page 487 !important

surreal pike
#

not working..

craggy ferry
#

If you try it on a different page with just a div element, does it work there? (Dont load the fonts, just apply css)

surreal pike
#

I think the problem is in the font-face definition..

craggy ferry
#

So it just doesn't show the fontfamily in element inspect?

surreal pike
#

yes

craggy ferry
#

weird, for me it adds it to the style but gives a warning that the font is not found

surreal pike
#

the font-face appears

craggy ferry
surreal pike
#

it works when I add the font-family through inspect

#

{pageNumbers?.map((page) => (
<style key={page}>
{ @font-face { font-family: 'Mushaf Page ${page}'; src: local('./fonts/mushaf-woff2/QCF_P${page}.woff2'), url(https://cdn.rawgit.com/mustafa0x/qpc-fonts/f93bf5f3/mushaf-woff2/QCF_P${page}.woff2) format('woff2'), url(https://cdn.rawgit.com/mustafa0x/qpc-fonts/f93bf5f3/mushaf-woff/QCF_P${page}.woff) format('woff'); } }

craggy ferry
#

Are you using the pages directory or app

surreal pike
#

I'm using react

#

plain react

craggy ferry
#

Oh okay

surreal pike
#

I don't like next

craggy ferry
#

I see

#

It also now does not show for me

surreal pike
#

so the problem doesn't have to do with mantine

craggy ferry
#

Nope, it has to do with the fontface but give me a sec, I need to pray and then will see how it could be fixed ๐Ÿ˜œ

surreal pike
#

ok

craggy ferry
#

I fixed it

#

I found the reason why it did not work

#

When you have a font family name with spaces, you need to put it in quotes

#

so "Mushaf Page ${page}"

#
fontFamily: `"Mushaf Page ${page}"`
#

@surreal pike

surreal pike
#

Thanks, a lot bro, you saved my day.

#

but what a silly mistake!

craggy ferry
#

I did not know either haha

surreal pike
#

I thought that the backticks would be a wrapper around it

#

I get it.
fontFamily: '"Mushaf page 444", sans-serif'

#

this will not work: fontFamily: 'Mushaf page 444, sans-serif'