Skip to content

[Footer]: Custom links are overwritten by default ones after the initial render #12296

Description

@radarfox

Description

I'm trying to integrate the footer component, but I'm having trouble customizing its content. I would like to pass custom data in links property. When I'm setting langList property the same way, everything works as expected. When I make a change in the source code and hot reload updates that module, the desired links suddenly show up. I'm using Next.js with @carbon/ibmdotcom-web-components.

Here is the example implementation:

import FooterContainer from '@carbon/ibmdotcom-web-components/es/components-react/footer/footer-container';
import { FC, useEffect, useRef } from 'react';

const langList = [
  { id: 'da', text: 'Danish / Dansk' },
  { id: 'nl', text: 'Dutch / Nederlands' },
  { id: 'en', text: 'English' },
];

const links = [
  {
    title: 'Quick links',
    links: [
      { title: 'About', url: '/about' },
      { title: 'Publications', url: '/publications' },
      { title: 'Blog', url: '/blog' },
      { title: 'Events', url: '/events' },
    ],
  },
  {
    title: 'Directories',
    links: [
      { title: 'Topics', url: '/topics' },
      { title: 'People', url: '/people' },
      { title: 'Projects', url: '/projects' },
    ],
  },
];

const Footer: FC = () => {
  const containerRef = useRef(null);

  useEffect(() => {
    const container = containerRef.current;
    if (!container) {
      return;
    }
    container.langList = langList;
    container.links = links;
  }, []);

  return <FooterContainer ref={containerRef} />;
};

export default Footer;

After debugging this problem for a while I have realized that the problem isn't in setting of the links, but rather for some reason after a few hundred miliseconds, the links will get overwritten by default links. With this knowledge I have come up with the following hotfix that seems to work. The only downside is the short "blink" as the default links are set and then changed back to custom ones. Some real fix from the Carbon team would be appriciated.

useEffect(() => {
  const container = containerRef.current as any;
  if (!container) {
    return;
  }
  // Set the custom links to the footer, this will immediately take effect.
  container.links = links;
  // The first custom title, that is expected to be displayed in the footer.
  const firstTitle = links[0].title;
  // The bug in the Carbon footer will cause the customized links to be
  // overwritten by a default links a few hundred milliseconds after the
  // initial rendering. Interval is set up to check when this happens.
  const intervalId = setInterval(() => {
    // Extract the text of the first title from the rendered footer.
    const firstRenderedTitle =
      container.children[0].children[1].children[0].getAttribute(
        'title-text',
      );
    // Compare the custom title with the rendered one. If they don't match, it
    // means that Carbon has already overwritten the links. If they do match,
    // do nothing and wait for the next invocation of the interval function.
    if (firstTitle !== firstRenderedTitle) {
      // Set the custom links for the second time, this time they won't be
      // overwritten and no further action is needed.
      container.links = links;
      // Stop invoking the interval, that checks the overwriting.
      clearInterval(intervalId);
    }
  }, 50);
  return () => {
    clearInterval(intervalId);
  };
}, []);

Component(s) impacted

Footer

Browser

Chrome

Carbon for IBM.com version

v2.28.0

Severity

Severity 3 = The problem is visible or noticeable to users but does not impede the usability or functionality. Affects minor functionality, has a workaround.

Application/website

https://research.ibm.com/

Package

@carbon/ibmdotcom-web-components

CodeSandbox example

See the description

Steps to reproduce the issue (if applicable)

No response

Release date (if applicable)

No response

Code of Conduct

Metadata

Metadata

Labels

bugSomething isn't workingdevNeeds some dev work

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions