How to attach variables to drupalSettings with ajax?
Use the SettingsCommand
$settings=['ajax_forms_test' =&gt; ['foo' =&gt; 42]]; /* An array of key/value pairs of JavaScript settings. */
$merge=TRUE; /* (Optional) Booleen Whether the settings should be merged into the global drupalSettings. */
$response-&gt;addCommand(new SettingsCommand($settings,$merge));

So your code would become something like this:
public static function AddAJAXResponseCommands(AjaxResponse $response = NULL, array $params = []) {

  $modalSettings = [
    'id' =&gt; 1,
    'status' =&gt; true,
  ];

  $sample = [
    '#type' =&gt; &quot;html_tag&quot;,
    '#tag' =&gt; &quot;div&quot;,
    '#attributes' =&gt; ['class' =&gt; ['context-menu']],
    '#value' =&gt; &quot;Dashboard&quot;,
  ];

  $sample['#attached']['library'][] = 'mylibrary/dashboard-js';
  $merge = TRUE;
  $settings['dashboard']['myVariables'] = $modalSettings;
  $response = new AjaxResponse();
  $response
    -&gt;addCommand(new ReplaceCommand('.context-menu', $sample))
    -&gt;addCommand(new SettingsCommand($settings, $merge));
  return $response;
}