<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Niti Agrawal</title>
    <description>The latest articles on DEV Community by Niti Agrawal (@niti_agrawal_1106).</description>
    <link>https://dev.to/niti_agrawal_1106</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2414074%2Fc978302b-6c87-4852-8284-79919c2ba929.jpg</url>
      <title>DEV Community: Niti Agrawal</title>
      <link>https://dev.to/niti_agrawal_1106</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niti_agrawal_1106"/>
    <language>en</language>
    <item>
      <title>Enhancing Accessibility: Managing Keyboard Navigation in Modals and Dropdowns</title>
      <dc:creator>Niti Agrawal</dc:creator>
      <pubDate>Wed, 27 Nov 2024 04:24:38 +0000</pubDate>
      <link>https://dev.to/niti_agrawal_1106/enhancing-accessibility-managing-keyboard-navigation-in-modals-and-dropdowns-4fj0</link>
      <guid>https://dev.to/niti_agrawal_1106/enhancing-accessibility-managing-keyboard-navigation-in-modals-and-dropdowns-4fj0</guid>
      <description>&lt;p&gt;Keyboard navigation is a critical aspect of web accessibility, especially for users relying on assistive technologies. One common challenge is ensuring that focus remains within a modal or dropdown when it is open. Here’s how you can implement focus and Escape key cycling functionality to create an accessible and user-friendly experience.&lt;/p&gt;

&lt;p&gt;The Problem:&lt;br&gt;
When a modal or dropdown is open:&lt;/p&gt;

&lt;p&gt;Tab Navigation: The focus should cycle within the component. Pressing the Tab key on the last focusable element should move focus back to the first, and Shift + Tab on the first element should return to the last.&lt;br&gt;
Escape to Close: Pressing the Escape key should close the modal or dropdown.&lt;br&gt;
The Solution:&lt;br&gt;
We can solve this with a generic approach that dynamically manages focusable elements within the container and ensures smooth keyboard interactions.&lt;/p&gt;

&lt;p&gt;Code Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getFocusableElements(container) {
  const focusableSelector = [
    'a[href]',
    'button:not([disabled])',
    'input:not([disabled]):not([type="hidden"])',
    'textarea:not([disabled])',
    'select:not([disabled])',
    '[tabindex]:not([tabindex="-1"])',
  ].join(', ');

  return Array.from(container.querySelectorAll(focusableSelector));
}

function enableFocusCycling(container, onCloseCallback) {
  let focusableElements = getFocusableElements(container);

  if (focusableElements.length === 0) {
    console.warn('No focusable elements found in the container');
    return;
  }

  const updateFocusableElements = () =&amp;gt; {
    focusableElements = getFocusableElements(container);
  };

  const first = () =&amp;gt; focusableElements[0];
  const last = () =&amp;gt; focusableElements[focusableElements.length - 1];

  container.addEventListener('keydown', (event) =&amp;gt; {
    if (event.key === 'Tab') {
      if (document.activeElement === last() &amp;amp;&amp;amp; !event.shiftKey) {
        event.preventDefault();
        first().focus();
        console.log('Focus cycled to the first element:', first());
      } else if (document.activeElement === first() &amp;amp;&amp;amp; event.shiftKey) {
        event.preventDefault();
        last().focus();
        console.log('Focus cycled to the last element:', last());
      }
    } else if (event.key === 'Escape') {
      event.preventDefault();
      if (onCloseCallback) onCloseCallback();
      console.log('Container closed via Escape key');
    }
  });

  // Update focusable elements dynamically if the content changes
  new MutationObserver(updateFocusableElements).observe(container, {
    childList: true,
    subtree: true,
  });
}

// Usage example
const container = document.querySelector('#myDiv');
enableFocusCycling(container, () =&amp;gt; {
  container.style.display = 'none'; // Replace with your hide logic
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Features:&lt;br&gt;
Focus Cycling: Ensures the Tab key keeps focus within the modal or dropdown.&lt;br&gt;
Escape Key Support: Adds a mechanism to close the modal or dropdown using the Escape key.&lt;br&gt;
Dynamic Content Handling: Uses MutationObserver to update focusable elements when the container’s content changes.&lt;br&gt;
Error Handling: Provides warnings for edge cases, such as no focusable elements in the container.&lt;br&gt;
Accessibility Best Practices:&lt;br&gt;
ARIA Attributes: Add proper ARIA roles (role="dialog", aria-modal="true", etc.) for better screen reader support.&lt;br&gt;
Keyboard Testing: Test across browsers to ensure consistent behavior.&lt;br&gt;
Graceful Degradation: Ensure functionality works even if JavaScript is disabled.&lt;br&gt;
Final Thoughts:&lt;br&gt;
This solution is a robust way to enhance accessibility for modals and dropdowns. By implementing seamless keyboard navigation, you not only meet accessibility standards but also improve the overall user experience for everyone.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>My Journey to Removing unsafe-eval from CSP: A Step-by-Step Guide</title>
      <dc:creator>Niti Agrawal</dc:creator>
      <pubDate>Tue, 12 Nov 2024 06:31:53 +0000</pubDate>
      <link>https://dev.to/niti_agrawal_1106/how-to-remove-unsafe-eval-from-content-security-policy-2pe3</link>
      <guid>https://dev.to/niti_agrawal_1106/how-to-remove-unsafe-eval-from-content-security-policy-2pe3</guid>
      <description>&lt;p&gt;Hello everyone! 👋&lt;/p&gt;

&lt;p&gt;This is my first post here. After facing significant challenges while trying to eliminate unsafe-eval from an existing application's Content Security Policy (CSP), I finally developed a robust and generic solution that other developers can easily adopt and extend. To support the dev community, I’m excited to share the steps I followed to accomplish this.&lt;/p&gt;

&lt;p&gt;Step 1: Replacing unsafe-eval Using mathjs&lt;br&gt;
I found that mathjs is a powerful tool that can replace the need for eval in evaluating mathematical expressions.&lt;br&gt;
Steps:&lt;br&gt;
&lt;strong&gt;Install mathjs:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install mathjs

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Import and configure mathjs:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { create, all } = require('mathjs');
const math = create(all);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Set up custom functions:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;math.import(
  {
    YOURFUNC: YOURFUNC,
    ADD: (a, b) =&amp;gt; a + b,
    parseInt
  },
  { override: true }
);

// Create a scope object
const scope = {
  x: 3,
  y: 4
};

try {
  evaluateValue = math.evaluate(exp, scope);
} catch (error) {
  // If `mathjs` cannot evaluate, proceed with the next approach
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success! 🎉 The code above should work for the following expressions, producing the same results as eval:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let exp;

exp = '1 + 2'; // Output: 3
exp = 'ADD(1, 2)'; // Output: 3
exp = '2 + 3 + parseInt(4.5)'; // Output: 9
exp = 'x * y' // Output: 12

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Handling Complex Expressions if mathjs Fails&lt;br&gt;
If mathjs cannot handle complex expressions that involve object properties, try this approach:&lt;/p&gt;

&lt;p&gt;Solution:&lt;br&gt;
Replace expressions dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const replacedExp = exp =&amp;gt; exp.replace(/myObject\["([^"]+)"\]/g, (_, key) =&amp;gt; JSON.stringify(data[key] || ''));

const myObject = {
  a: "test1",
  "a.b": "test2",
  "a.b.c": "test3",
  2: "test4"
};

// Examples
console.log(myObject["a"]);       // Output: "test1"
console.log(myObject["a.b"]);     // Output: "test2"
console.log(myObject["a.b.c"]);   // Output: "test3"
console.log(myObject[2]);         // Output: "test4"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach should help you safely handle complex object expressions without resorting to eval.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
