Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

insted of click , iwant details to be displayed on hover //…

  1. User

    insted of click , iwant details to be displayed on hover

    // src/components/AchievementsTimeline.js

    import React, { useEffect, useState } from 'react'; import { useStaticQuery, graphql } from 'gatsby'; import styled from 'styled-components'; import { srConfig } from '@config'; import sr from '@utils/sr'; import { usePrefersReducedMotion } from '@hooks';

    // Styled Components

    const StyledAchievementsSection = styled.section` max-width: 900px; margin: 0 auto; padding: 4rem 0;

    .heading { text-align: center; margin-bottom: 3rem; font-size: 2.5rem; position: relative;

    &::after {
      content: '';
      width: 60px;
      height: 4px;
      background: var(--green);
      display: block;
      margin: 0.75rem auto 0;
      border-radius: 2px;
    }
    

    } `;

    const Timeline = styled.ol` position: relative; padding: 0; list-style: none;

    &::before { content: ''; position: absolute; left: 50%; top: 0; bottom: 0; width: 4px; background: var(--lightest-navy); transform: translateX(-50%); }

    @media (max-width: 768px) { &::before { left: 20px; transform: none; } } `;

    const TimelineItem = styled.li` position: relative; width: 50%; padding: 1rem 2rem; box-sizing: border-box; cursor: pointer; transition: background 0.3s ease;

    &:nth-child(odd) { left: 0; text-align: right; }

    &:nth-child(even) { left: 50%; text-align: left; }

    &:nth-child(odd)::before { content: ''; position: absolute; top: 1.5rem; right: -8px; width: 16px; height: 16px; background: var(--green); border: 4px solid var(--lightest-navy); border-radius: 50%; z-index: 1; }

    &:nth-child(even)::before { content: ''; position: absolute; top: 1.5rem; left: -8px; width: 16px; height: 16px; background: var(--green); border: 4px solid var(--lightest-navy); border-radius: 50%; z-index: 1; }

    .content { background: var(--lightest-navy); padding: 1.5rem; border-radius: var(--border-radius); position: relative; transition: background 0.3s ease;

    &:hover {
      background: var(--navy);
    }
    
    &.expanded {
      background: var(--navy);
    }
    

    }

    .title { font-size: var(--fz-lg); font-weight: 600; margin-bottom: 0.5rem; color: var(--green); }

    .company { font-size: var(--fz-md); color: var(--slate); margin-bottom: 0.5rem; }

    .range { font-size: var(--fz-sm); color: var(--light-slate); margin-bottom: 0.5rem; }

    .location { font-size: var(--fz-sm); color: var(--light-slate); }

    .description { margin-top: 1rem; font-size: var(--fz-sm); color: var(--light-slate); max-height: 0; overflow: hidden; transition: max-height 0.3s ease; }

    &.expanded .description { max-height: 500px; /* Adjust as needed */ }

    @media (max-width: 768px) { width: 100%; left: 0; text-align: left; padding-left: 3rem; margin-bottom: 2rem; cursor: default;

    &:nth-child(odd),
    &:nth-child(even) {
      left: 0;
    }
    
    &:nth-child(odd)::before,
    &:nth-child(even)::before {
      left: 12px;
    }
    

    } `;

    const AchievementsTimeline = () => { const data = useStaticQuery(graphql query { achievements: allMarkdownRemark( filter: { fileAbsolutePath: { regex: "/content/Volunteer/" } } sort: { fields: [frontmatter___date], order: DESC } ) { edges { node { frontmatter { title company location range url } html } } } } );

    const achievementsData = data.achievements.edges;

    const prefersReducedMotion = usePrefersReducedMotion();

    // State to track which items are expanded const [expandedIndex, setExpandedIndex] = useState(null);

    useEffect(() => { if (prefersReducedMotion) { return; }

    sr.reveal('.timeline-item', srConfig());
    

    }, []);

    // Handler to toggle expand/collapse const toggleExpand = (index) => { if (expandedIndex === index) { setExpandedIndex(null); // Collapse if already expanded } else { setExpandedIndex(index); // Expand the clicked item } };

    return ( Achievements {achievementsData.map(({ node }, index) => { const { title, company, location, range, url } = node.frontmatter; const isExpanded = expandedIndex === index;

          return (
            <TimelineItem
              key={index}
              className={`timeline-item ${isExpanded ? 'expanded' : ''}`}
              onClick={() => toggleExpand(index)}
              role="button"
              tabIndex={0}
              onKeyPress={(e) => {
                if (e.key === 'Enter' || e.key === ' ') {
                  toggleExpand(index);
                }
              }}
              aria-expanded={isExpanded}
              aria-controls={`achievement-details-${index}`}
            >
              <div className={`content ${isExpanded ? 'expanded' : ''}`}>
                <h3 className="title">
                  {title} @{' '}
                  <a href={url} className="inline-link">
                    {company}
                  </a>
                </h3>
                <p className="range">{range}</p>
                <p className="location">{location}</p>
                <div
                  id={`achievement-details-${index}`}
                  className="description"
                  dangerouslySetInnerHTML={{ __html: node.html }}
                />
              </div>
            </TimelineItem>
          );
        })}
      </Timeline>
    </StyledAchievementsSection>
    

    ); };

    export default AchievementsTimeline;

  2. Assistant

    To modify your AchievementsTimeline component so that the achievement details are displayed on hover instead of on click, you'll need to adjust both the component's state management and its styling. Here's a step-by-step guide to achieve this:

    1. Remove State Management for Expansion: Since the expansion will now be handled purely through CSS on hover, you won’t need to track the expansion state in React. This means you can remove the state variables and related handlers.

    2. Update Event Handlers: Remove the onClick, onKeyPress, and any accessibility attributes related to toggling the expansion.

    3. Adjust Styled Components: Modify the styled components to reveal the .description section when the parent TimelineItem is hovered.

    4. Enhance Accessibility: While hover-based interactions are great for desktop users, it's essential to ensure that keyboard and assistive technology users can still access the details. We'll add focus styles to mimic hover behavior for keyboard navigation.

    Here's the updated AchievementsTimeline.js with these changes implemented:

    // src/components/AchievementsTimeline.js
    
    import React, { useEffect } from 'react';
    import { useStaticQuery, graphql } from 'gatsby';
    import styled from 'styled-components';
    import { srConfig } from '@config';
    import sr from '@utils/sr';
    import { usePrefersReducedMotion } from '@hooks';
    
    // Styled Components
    
    const StyledAchievementsSection = styled.section`
      max-width: 900px;
      margin: 0 auto;
      padding: 4rem 0;
    
      .heading {
        text-align: center;
        margin-bottom: 3rem;
        font-size: 2.5rem;
        position: relative;
    
        &::after {
          content: '';
          width: 60px;
          height: 4px;
          background: var(--green);
          display: block;
          margin: 0.75rem auto 0;
          border-radius: 2px;
        }
      }
    `;
    
    const Timeline = styled.ol`
      position: relative;
      padding: 0;
      list-style: none;
    
      &::before {
        content: '';
        position: absolute;
        left: 50%;
        top: 0;
        bottom: 0;
        width: 4px;
        background: var(--lightest-navy);
        transform: translateX(-50%);
      }
    
      @media (max-width: 768px) {
        &::before {
          left: 20px;
          transform: none;
        }
      }
    `;
    
    const TimelineItem = styled.li`
      position: relative;
      width: 50%;
      padding: 1rem 2rem;
      box-sizing: border-box;
      transition: background 0.3s ease;
    
      &:nth-child(odd) {
        left: 0;
        text-align: right;
      }
    
      &:nth-child(even) {
        left: 50%;
        text-align: left;
      }
    
      &:nth-child(odd)::before {
        content: '';
        position: absolute;
        top: 1.5rem;
        right: -8px;
        width: 16px;
        height: 16px;
        background: var(--green);
        border: 4px solid var(--lightest-navy);
        border-radius: 50%;
        z-index: 1;
      }
    
      &:nth-child(even)::before {
        content: '';
        position: absolute;
        top: 1.5rem;
        left: -8px;
        width: 16px;
        height: 16px;
        background: var(--green);
        border: 4px solid var(--lightest-navy);
        border-radius: 50%;
        z-index: 1;
      }
    
      .content {
        background: var(--lightest-navy);
        padding: 1.5rem;
        border-radius: var(--border-radius);
        position: relative;
        transition: background 0.3s ease, max-height 0.3s ease;
    
        /* Reveal the description on hover or focus */
        &:hover,
        &:focus-within {
          background: var(--navy);
        }
    
        /* Ensure the description is visible on hover */
        &:hover .description,
        &:focus-within .description {
          max-height: 500px; /* Adjust as needed */
        }
      }
    
      .title {
        font-size: var(--fz-lg);
        font-weight: 600;
        margin-bottom: 0.5rem;
        color: var(--green);
      }
    
      .company {
        font-size: var(--fz-md);
        color: var(--slate);
        margin-bottom: 0.5rem;
      }
    
      .range {
        font-size: var(--fz-sm);
        color: var(--light-slate);
        margin-bottom: 0.5rem;
      }
    
      .location {
        font-size: var(--fz-sm);
        color: var(--light-slate);
      }
    
      .description {
        margin-top: 1rem;
        font-size: var(--fz-sm);
        color: var(--light-slate);
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease;
      }
    
      @media (max-width: 768px) {
        width: 100%;
        left: 0;
        text-align: left;
        padding-left: 3rem;
        margin-bottom: 2rem;
    
        &:nth-child(odd),
        &:nth-child(even) {
          left: 0;
        }
    
        &:nth-child(odd)::before,
        &:nth-child(even)::before {
          left: 12px;
        }
      }
    `;
    
    const AchievementsTimeline = () => {
      const data = useStaticQuery(graphql`
        query {
          achievements: allMarkdownRemark(
            filter: { fileAbsolutePath: { regex: "/content/Volunteer/" } }
            sort: { fields: [frontmatter___date], order: DESC }
          ) {
            edges {
              node {
                frontmatter {
                  title
                  company
                  location
                  range
                  url
                }
                html
              }
            }
          }
        }
      `);
    
      const achievementsData = data.achievements.edges;
    
      const prefersReducedMotion = usePrefersReducedMotion();
    
      useEffect(() => {
        if (prefersReducedMotion) {
          return;
        }
    
        sr.reveal('.timeline-item', srConfig());
      }, [prefersReducedMotion]);
    
      return (
        <StyledAchievementsSection id="achievements">
          <h2 className="heading">Achievements</h2>
          <Timeline>
            {achievementsData.map(({ node }, index) => {
              const { title, company, location, range, url } = node.frontmatter;
    
              return (
                <TimelineItem
                  key={index}
                  className="timeline-item"
                >
                  <div className="content" tabIndex="0">
                    <h3 className="title">
                      {title} @{' '}
                      <a href={url} className="inline-link" target="_blank" rel="noopener noreferrer">
                        {company}
                      </a>
                    </h3>
                    <p className="range">{range}</p>
                    <p className="location">{location}</p>
                    <div
                      id={`achievement-details-${index}`}
                      className="description"
                      dangerouslySetInnerHTML={{ __html: node.html }}
                    />
                  </div>
                </TimelineItem>
              );
            })}
          </Timeline>
        </StyledAchievementsSection>
      );
    };
    
    export default AchievementsTimeline;
    

    Detailed Changes Explained:

    1. Removed State Management:

      • Removed:
        const [expandedIndex, setExpandedIndex] = useState(null);
        
        and the toggleExpand function.
      • Removed Event Handlers: The onClick, onKeyPress, aria-expanded, and aria-controls props from TimelineItem have been removed since we're no longer managing expansion through React state.
    2. Updated Styled Components for Hover Behavior:

      • .content Styles:

        &:hover,
        &:focus-within {
          background: var(--navy);
        }
        
        &:hover .description,
        &:focus-within .description {
          max-height: 500px; /* Adjust as needed */
        }
        

        These styles ensure that when a user hovers over the .content div or when it receives focus (for keyboard navigation), the background changes, and the .description section expands by increasing its max-height.

      • .description Styles:

        .description {
          margin-top: 1rem;
          font-size: var(--fz-sm);
          color: var(--light-slate);
          max-height: 0;
          overflow: hidden;
          transition: max-height 0.3s ease;
        }
        

        The .description is initially hidden by setting max-height: 0. On hover or focus, its max-height increases, making it visible with a smooth transition.

    3. Accessibility Enhancements:

      • Tab Navigation: Added tabIndex="0" to the .content div to make it focusable via keyboard.
        <div className="content" tabIndex="0">
        
      • Keyboard Accessibility: By using :focus-within, the detailed description will also appear when the content div is focused via keyboard navigation, ensuring that users who navigate using the keyboard can access the details.
    4. Additional Enhancements:

      • Link Accessibility: Added target="_blank" and rel="noopener noreferrer" to the company link to open it in a new tab safely.
        <a href={url} className="inline-link" target="_blank" rel="noopener noreferrer">
          {company}
        </a>
        
    5. Scroll Reveal (sr) Integration: Since the expansion is now handled via CSS hover and focus, the scroll reveal still applies to the .timeline-item for animations as they come into view.

    Final Notes:

    • Responsive Design: The media queries ensure that on smaller screens (e.g., mobile devices), the timeline adjusts accordingly, maintaining usability and design integrity.

    • Transition Adjustments: You can tweak the max-height in the .description hover state to accommodate longer or shorter content as needed.

    • Testing: After making these changes, thoroughly test the component across different devices and input methods (mouse, keyboard, touch) to ensure a smooth and accessible user experience.

    By following these adjustments, your AchievementsTimeline component will now display the achievement details when a user hovers over an item or focuses on it via keyboard navigation, providing an intuitive and interactive experience.